Powershell 后台作业中复制项目的凭据错误
我有一个PowerShell脚本,作为后台作业调用多台服务器上的函数 在该功能中,有一个Powershell 后台作业中复制项目的凭据错误,powershell,Powershell,我有一个PowerShell脚本,作为后台作业调用多台服务器上的函数 在该功能中,有一个复制项,它总是失败,并显示以下错误消息: 用户名或密码不正确。 我要将项目复制到的目标是synology NAS上的网络共享 这有点奇怪,因为运行后台作业的用户在默认情况下拥有对目标共享的完全访问权限(我通过将whoami输出到日志文件来确保确实是该用户运行它) 但是,我还明确地将我的凭据再次加载到后台作业脚本块中,并创建了一个到目标的PSDrive,以确保我确实具有访问权限,并且我还将凭据添加到我的Invo
复制项
,它总是失败,并显示以下错误消息:
用户名或密码不正确。
我要将项目复制到的目标是synology NAS上的网络共享
这有点奇怪,因为运行后台作业的用户在默认情况下拥有对目标共享的完全访问权限(我通过将whoami
输出到日志文件来确保确实是该用户运行它)
但是,我还明确地将我的凭据再次加载到后台作业脚本块中,并创建了一个到目标的PSDrive,以确保我确实具有访问权限,并且我还将凭据添加到我的Invoke命令中。发生什么事了
这是我的密码:
$Sources = @{
"HV-Server" = Invoke-Command -ComputerName DC { Get-ADComputer -Filter "Name -like 'HV*'" } | Where-Object {
Test-Connection $_.Name -Count 1 -ErrorAction SilentlyContinue
} | Select-Object -ExpandProperty Name
}
$Destinations = @{
"default" = "\\nas\Backup\VHDX"
}
. "\\server\install$\Powershell-Scripts\Functions\Security\Get-CustomCredential.ps1"
$Cred = Get-CustomCredential domain\myuser
Function Copy-VHDXToNAS {
Param(
[hashtable]$DestinationTable
)
Import-Module "Hyper-V"
$BackupResult = Get-VM | Where-Object { $_.Name -notlike 'nob*' } | Get-VMHardDiskDrive |
Where-Object { !$_.Path.StartsWith("\\") } | ForEach-Object {
. "\\server\install$\Powershell-Scripts\Functions\Security\Get-CustomCredential.ps1"
$Cred = Get-CustomCredential domain\myuser
$Destination = $DestinationTable.Get_Item($_.VMName)
if (!$Destination) { $Destination = $DestinationTable.Get_Item("default") }
New-PSDrive Nas -Root $Destination -PSProvider FileSystem -Credential $Cred > $null
$Destination = Join-Path $Destination $_.VMName
New-Item $Destination -ItemType Directory -ErrorAction SilentlyContinue > $null
Try {
Copy-Item $_.Path $Destination -Force -ErrorAction Stop
$Result = "SUCCESS"
}
Catch {
$a = whoami
$Result = "ERROR: " + $_.Exception.Message + $a
}
[PSCustomObject]@{
VMName = $_.VMName
VHDXName = Split-Path $_.Path -Leaf
Destination = $Destination
Result = $Result
}
}
$BackupResult
}
$AllHosts = $Sources.Get_Item("HV-Server")
$Jobs = foreach ($HVHost in $AllHosts) {
Invoke-Command -ComputerName $HVHost -ScriptBlock ${function:Copy-VHDXToNAS} -ArgumentList $Destinations -AsJob -Credential $Cred
}
$JobResult = $Jobs | Wait-Job | Receive-Job
正如DougMaurer所提到的,这确实是一个次要问题。我认为通过在Scriptblock中更新凭据,我不会有第二跳问题,但我做到了
在文章中,他们讨论了第二跳问题以及如何解决它。我使用了PSSessionConfiguration
方式
基本上,您可以使用以下命令在远程服务器上创建会话配置:
$ConfigurationSplat = @{
Name = "Your.ConfigurationName"
Force = $true
MaximumReceivedDataSizePerCommandMB = 200MB
MaximumReceivedObjectSizeMB = 200MB
RunAsCredential = $Cred
}
Register-PSSessionConfiguration @ConfigurationSplat
然后您可以在Invoke命令中使用ConfigurationName
,如下所示:
Invoke-Command -ComputerName $HVHost -ConfigurationName "Your.ConfigurationName" -ScriptBlock ${function:Copy-VHDXToNAS} -ArgumentList $Destinations -AsJob -Credential $Cred
这就解决了第二跳的问题。正如DougMaurer所提到的,这确实是第二跳的问题。我认为通过在Scriptblock中更新凭据,我不会有第二跳问题,但我做到了
在文章中,他们讨论了第二跳问题以及如何解决它。我使用了PSSessionConfiguration
方式
基本上,您可以使用以下命令在远程服务器上创建会话配置:
$ConfigurationSplat = @{
Name = "Your.ConfigurationName"
Force = $true
MaximumReceivedDataSizePerCommandMB = 200MB
MaximumReceivedObjectSizeMB = 200MB
RunAsCredential = $Cred
}
Register-PSSessionConfiguration @ConfigurationSplat
然后您可以在Invoke命令中使用ConfigurationName
,如下所示:
Invoke-Command -ComputerName $HVHost -ConfigurationName "Your.ConfigurationName" -ScriptBlock ${function:Copy-VHDXToNAS} -ArgumentList $Destinations -AsJob -Credential $Cred
这就解决了第二跳的问题。你在域环境上吗?@AbrahamZinala是的。我连接到的所有服务器都在域环境中。这似乎只是一个双跳问题。如何将脚本作为后台作业运行?你使用任务调度器吗?@DougMaurer我也在考虑这个问题,但我认为如果我在脚本块中导入我的凭据,那应该不再是问题了。不过我查了一下,你在域名环境上吗?@AbrahamZinala是的。我连接到的所有服务器都在域环境中。这似乎只是一个双跳问题。如何将脚本作为后台作业运行?你使用任务调度器吗?@DougMaurer我也在考虑这个问题,但我认为如果我在脚本块中导入我的凭据,那应该不再是问题了。不过,我会进一步研究这个问题