如果我单独启动两个脚本,它们可以完美运行。脚本1:
for i in sent.split('!')[:-1]:
print(i+'!')
I am honest!
I do!
I do!
I really do!
脚本2
$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)
但是,如果我现在将第一个脚本放在第二个脚本之前,它将中止并显示一条错误消息。但是,由于给出了所有参数,因此我无法全部理解。目的是仅在可以访问服务器时执行清单脚本。
这是我的失败:
[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
}
}
}
答案 0 :(得分:2)
param伪指令必须始终在脚本中第一。如果您将这两个脚本结合在一起,请将此部分移到文件顶部:
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[string[]]$ComputerName = $Env:ComputerName,
[string]$OutputFile = "\\server01\temp\ergebnis.csv"
)