[英]PowerShell: Test-Connection
如果我單獨啟動它們,我有兩個運行完美的腳本。 腳本 1:
$serverName = "server01"
Do
{
Test-Connection -ComputerName $serverName -ErrorAction SilentlyContinue -ErrorVariable pingError |
Out-Null
$pingError = $pingError | Where-Object { $_.CategoryInfo.Category -Eq 'ResourceUnavailable' }
Start-Sleep -Seconds 5
}
While($pingError -Ne $Null)
腳本 2
[cmdletbinding()]
param(
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername,
[string]$OutputFile = "\\server01\temp\ergebnis.csv"
)
$Model = Get-WmiObject Win32_ComputerSystem | Select -Expand Model
function Get-InstalledApps
# This function will loop through the applications installed on one PC
# and output one object for each Application with all its properties.
# optionally saving/appending to a .CSV spreadsheet.
{
foreach ($App in $Applications)
{
$AppRegistryKey = $UninstallRegKey + "\\" + $App
$AppDetails = $HKLM.OpenSubKey($AppRegistryKey)
#$AppGUID = $App
if (($($AppDetails.GetValue("DisplayName")) -notlike "Security Update*") -and
($($AppDetails.GetValue("DisplayName")) -notlike "Microsoft App Update for*") -and
($($AppDetails.GetValue("DisplayName")) -notlike "Update for Microsoft*") )
{
$AppDisplayName = $($AppDetails.GetValue("DisplayName"))
$AppVersion = $($AppDetails.GetValue("DisplayVersion"))
#$AppPublisher = $($AppDetails.GetValue("Publisher"))
#$AppInstalledDate = $($AppDetails.GetValue("InstallDate"))
#$AppUninstall = $($AppDetails.GetValue("UninstallString"))
if(!$AppDisplayName) { continue }
$OutputObj = New-Object -TypeName PSobject
#$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value
$Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name AppName -Value $AppDisplayName
$OutputObj | Add-Member -MemberType NoteProperty -Name AppVersion -Value $AppVersion
#$OutputObj | Add-Member -MemberType NoteProperty -Name AppVendor -Value $AppPublisher
#$OutputObj | Add-Member -MemberType NoteProperty -Name InstalledDate -Value $AppInstalledDate
$OutputObj | Add-Member -MemberType NoteProperty -Name Typ -Value $Model
#$OutputObj | Add-Member -MemberType NoteProperty -Name UninstallKey -Value $AppUninstall
#$OutputObj | Add-Member -MemberType NoteProperty -Name AppGUID -Value $AppGUID
#if ($RegistryView -eq 'Registry32')
#{
#$OutputObj | Add-Member -MemberType NoteProperty -Name Arch -Value '32'
#} else {
# $OutputObj | Add-Member -MemberType NoteProperty -Name Arch -Value '64'
#}
$OutputObj
# Export to a file
$OutputObj | export-csv -append -NoType -Encoding UTF8 -Delimiter ";" -path $OutputFile
}
}
}
if((Test-Path "\\server01\temp") -eq $false){
New-Item -Path "\\server01\temp" -ItemType Directory -Force}
$UninstallRegKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
Remove-Item $OutputFile -ErrorAction SilentlyContinue
foreach($Computer in $ComputerName)
{
Write-Output "Computer: $Computer"
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0)
{
# Get the architecture 32/64 bit
if ((Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer -ea 0).OSArchitecture -eq '64-bit')
{
# If 64 bit check both 32 and 64 bit locations in the registry
$RegistryViews = @('Registry32','Registry64')
} else {
# Otherwise only 32 bit
$RegistryViews = @('Registry32')
}
foreach ( $RegistryView in $RegistryViews )
{
# Get the reg key(s) where add/remove program information is stored.
$HKLM = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computer,$RegistryView)
$UninstallRef = $HKLM.OpenSubKey($UninstallRegKey)
$Applications = $UninstallRef.GetSubKeyNames()
# Now we have the registry locations, call the function which will enumerate
# all the applications on this PC
Get-InstalledApps
}
}
}
但是,如果我現在將第一個腳本放在第二個腳本之前,它將中止並顯示一條錯誤消息。 但是,我無法弄清所有這些,因為所有參數都已給出。 目的是僅在可以訪問服務器時才執行清單腳本。
這是我的失敗:
In Zeile:11 Zeichen:1
+ [cmdletbinding()]
+
Unerwartetes Attribut "cmdletbinding".
In Zeile:12 Zeichen:1
+ param(
Unexpected token "param" in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedAttribute
param指令必須始終在腳本中排在第一位。 如果合並這兩個腳本,請將此部分移至文件頂部:
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[string[]]$ComputerName = $Env:ComputerName,
[string]$OutputFile = "\\server01\temp\ergebnis.csv"
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.