【发布时间】:2017-09-02 08:23:19
【问题描述】:
所以我使用 powershell 脚本构建 Json 文件,以确保所有域站点的所有软件版本都是最新的(使用不同域名的几个不同数据中心。我有一个特殊的 vpn信任所有域,因此我可以通过我的笔记本电脑毫无问题地访问所有域。
我起初认为这是一个凭据问题,所以我删除了该部分,但它似乎是网络级别的,我不确定为什么我可以 RDP、浏览到网络路径等。
连接到远程服务器 servera 失败,出现以下错误 消息:WinRM 无法处理该请求。以下错误 使用 Kerberos 身份验证时发生:找不到计算机 服务器验证计算机是否存在于网络中,并且 提供的名称拼写正确。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。 + CategoryInfo : OpenError: (servera:String) [], PSRemotingTransportException + FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("Servers")]
[string[]]$Name = (Get-Content "c:\versioncheck\servers.txt"),
[string]$Credential = "svcStatusCheck",
[string]$PathToCred = "c:\versioncheck"
)
Begin {
Function Get-Credentials {
Param (
[String]$AuthUser = $env:USERNAME
)
$Key = [byte]29, 36, 18, 74, 72, 75, 85, 52, 73, 44, 0, 21, 98, 76, 99, 28
$CredFile = $AuthUser.Replace("\", "~")
$File = $PathToCred + "\Credentials-$CredFile.crd"
If (-not (Test-Path $File)) {
(Get-Credential $AuthUser).Password | ConvertFrom-SecureString -Key $Key | Set-Content $File
}
$Password = Get-Content $File | ConvertTo-SecureString -Key $Key
$AuthUser = (Split-Path $File -Leaf).Substring(12).Replace("~", "\")
$AuthUser = $AuthUser.Substring(0, $AuthUser.Length - 4)
$Credential = New-Object System.Management.Automation.PsCredential($AuthUser, $Password)
Return $Credential
}
}
Process {
$AllServers = @()
ForEach ($Computer in $Name) {
$AllServers += $Computer
}
}
End {
ForEach ($Computer in $AllServers) {
Write-Host "$(Get-Date): Script begins!"
write-host "Checking $computer"
Invoke-Command -cn $computer -credential $Credential -ScriptBlock { Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object @{Label = "ServerName"; Expression = {$env:computername}}, DisplayName, Publisher, DisplayVersion,InstallDate | Where-object { $_.Publisher -match "Software" } | ConvertTo-Json -depth 100 | Out-File "c:\versioncheck\InstalledApps.json"}
}
}
【问题讨论】:
-
一些旁白:
[byte]29, 36, 18, 74, 72, 75, 85, 52, 73, 44, 0, 21, 98, 76, 99, 28不会创建[byte[]]数组 - 使用[byte[]] (29, 36, ...)。您的process块无法按预期使用管道输入 - 将$AllServers = @()放入您的begin块中,并在您的$AllServers += $Name块中使用process。 -
另外:最好avoid
Write-Host。
标签: powershell