Powershell 中的变量问题

issue with variable in Powershell

您好,我正在使用下面的脚本块,并且在测试路径时传递了变量本地文件夹 $Null。你能建议我在这里缺少什么吗

$ScriptBlockDir

在您的示例中(除非您想提供更多背景信息)您不需要 -Session $PSSession,因为您的代码中声明了 none,它会抛出有关 $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