您好我正在使用下面的scriptblock并且已经传递了变量本地文件夹,而在测试路径时它将来$Null
。你能告诉我这里缺少什么
$ScriptBlockDir = {
Param (
[string]$samAccountName
)
If ($Env:ComputerName -eq 'fileserver101' )
{
$LocalFolderPath = 'H:\Users'
}
Else
{
$LocalFolderPath = 'D:\Users'
}
If (-not (test-path -Path "$LocalFolderPath\$samAccountName"))
{
Try
{
LogEntry "New Home Drive Folder"
New-Item -Path $LocalFolderPath -Name $samAccountName -ItemType Directory >$Null
return 1
}
catch
{
Write-Host "Failed with error"
return -1
}
}
Write-Host "already exists"
Return 0
}
$result = Invoke-Command -Session $PSSession -ScriptBlock $ScriptBlockDir -ArgumentList $samAccountName
答案 0 :(得分:0)
在你的例子中(除非你想提供更多背景知识)你不需要-Session $ PSSession,因为你的代码中没有声明,它会抛出关于$ null变量的错误。我会像这样重做你的代码:
$ScriptBlockDir = {
Param ([string]$samAccountName)
If ($Env:ComputerName -eq 'fileserver101' ) {
$LocalFolderPath = 'H:\Users'
}
Else {
$LocalFolderPath = 'D:\Users'
}
If (-not (Test-Path -Path "$LocalFolderPath\$samAccountName")) {
try {
New-Item -Path $LocalFolderPath -Name $samAccountName -ItemType Directory -ErrorAction Stop
return 1
}
catch {
Write-Host $_.Exception.Message
return -1
}
}
else {
Write-Host "already exists"
Return 0
}
}
$result = Invoke-Command -ScriptBlock $ScriptBlockDir -ArgumentList $samAccountName
Write-Host $result