2017-11-06 3 views
0

こんにちは私は以下のスクリプトブロックを使用していて、テストパス中に$Nullという変数ローカルフォルダを渡しました。あなたは、あなたのコード内で宣言した。存在しないとして、あなたが-Session $のPSSessionを必要としない(あなたはより多くの背景を提供したいと考えていない限り)私はあなたの例では、ここでPowershellで変数付きの問題

$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

行方不明です何を提案してください可能性があり、それがスローされます変数$ 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 
関連する問題