如何使用此PS脚本



我有几个PS脚本文件,它们是为执行任务而创建的。问题是,我不知道如何使用它们来从中提取我需要的信息,或者进行脚本应该进行的更改。当我右键单击并使用PowerShell运行时,我会短暂打开一个PS窗口,然后关闭。在下面的代码中,我想提取域上多台计算机的操作系统版本,并将其与计算机名称一起列出。

这意味着,如果我知道它们上有什么版本的Windows,我就不必去访问每台机器来重新制作很多。

有人想和一个新手一起工作并向我展示如何使用PS文件来实现这一点吗?

.SYNOPSIS
Get SDC version from USAF computers.
.PARAMETER ComputerName
Specifies the computers to query.
.PARAMETER IncludeError
Optional switch to include nonresponding computers.
.INPUTS
None. You cannot pipe objects.
.OUTPUTS
System.Object
.EXAMPLE
.Get-SDCVersion
.EXAMPLE
.Get-SDCVersion -ComputerName PC01,PC02,PC03
.EXAMPLE
.Get-SDCVersion (Get-Content C:computers.txt) -ErrorAction SilentlyContinue
.EXAMPLE
.Get-SDCVersion (Get-Content C:computers.txt) -IncludeNonResponding -Verbose |
Export-Csv SDCVersion.csv -NoTypeInformation
.NOTES
Author: Matthew D. Daugherty
Date Modified: 2 August 2020
#>
[CmdletBinding()]
param (
[Parameter()]
[string[]]
$ComputerName = $env:COMPUTERNAME,
[Parameter()]
[switch]
$IncludeNonResponding
)
# Scriptblock for Invoke-Command
$InvokeCommandScriptBlock = {
$VerbosePreference = $Using:VerbosePreference

Write-Verbose "Getting SDC version on $env:COMPUTERNAME."
$Path = 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionOEMInformation'
[PSCustomObject]@{
ComputerName = $env:COMPUTERNAME
SDCVersion = (Get-ItemProperty -Path $Path).Model
}
} 
# Parameters for Invoke-Command
$InvokeCommandParams = @{
ComputerName = $ComputerName
ScriptBlock = $InvokeCommandScriptBlock
ErrorAction = $ErrorActionPreference
}
switch ($IncludeNonResponding.IsPresent) {
'True' {
$InvokeCommandParams.Add('ErrorVariable','NonResponding')
Invoke-Command @InvokeCommandParams | 
Select-Object -Property *, ErrorId -ExcludeProperty PSComputerName, PSShowComputerName, RunspaceId
if ($NonResponding) {
foreach ($Computer in $NonResponding) {
[PSCustomObject]@{
ComputerName = $Computer.TargetObject.ToUpper()
SDCVersion = $null
ErrorId = $Computer.FullyQualifiedErrorId
}
}
}
}
'False' {
Invoke-Command @InvokeCommandParams | 
Select-Object -Property * -ExcludeProperty PSComputerName, PSShowComputerName, RunspaceId
}
}

您可以通过打开一个PS窗口来测试这一点,并从那里运行脚本,而不是运行脚本文件。

导入模块ActiveDirectory

获取ADComputer-SearchBase";OU=计算机帐户,DC=foo,DC=bar,DC=more,DC=stuff&quot-filter*-properties operatingSystem|选择对象名称,operatingSystem|export CSV\Domain\shares\Temp\Computers.CSV-notype

如果希望输出保持在窗口中,请先打开PS窗口,然后取下"|导出CSV\Domain\shares\Temp\Computers.CSV-notype"这样输出就不会被重定向。

如果Powershell窗口立即打开和关闭,则表示存在错误或脚本已完成执行。您可以在脚本末尾添加读取主机-提示"按任意键">或者您可以打开Powershell窗口,转到文件夹(使用设置位置-路径"…..">(,然后执行脚本。\我的脚本.ps1

最新更新