[英]issue with variable in Powershell
Hello I am using below scriptblock and have passed variable local folder where while testing path it is coming $Null
.您好,我正在使用下面的脚本块,并且已经通过了变量本地文件夹,在测试路径时,它是
$Null
。 could you please suggest what I am missing here你能建议我在这里缺少什么吗
$ScriptBlockDir
In your example (unless you would like to provide more background) you don't need -Session $PSSession as there is none declared in your code and it will throw error about $null variable.在您的示例中(除非您想提供更多背景信息),您不需要 -Session $PSSession ,因为您的代码中没有声明任何内容,它会引发有关 $null 变量的错误。 I would redo your code like this:
我会像这样重做你的代码:
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.