0

Hello I am using below scriptblock and have passed variable local folder where while testing path it is coming $Null. could you please suggest what I am missing here

$ScriptBlockDir
Suuny
  • 117
  • 1
  • 1
  • 9

1 Answers1

0

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. 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
Iggy Zofrin
  • 515
  • 3
  • 10