Home > Net >  Powershell Export-CSV with the path as a variable
Powershell Export-CSV with the path as a variable

Time:08-17

I have a script that displays the Active Directory computers in a CSV list. If I define the path for Export-CSV as plain text, everything works fine. However, when I try to recreate the path with variables using Join-Path, I get the error that the argument cannot be bound to the "Path" parameter because it is NULL.

Does anyone know how i could solve the problem?

Script:

$Export_Path_External = "\\${env:COMPUTERNAME}."   "${env:USERDNSDOMAIN}"   "\C$\users\bob"

$AD_Server_Name = 'ADServer.local'

$Administrator = 'Domain\Administrator'

Invoke-Command -ComputerName $AD_Server_Name -Credential $Administrator -ScriptBlock {
$AD_Clients = Get-ADComputer -Filter 'Name -like "PC-*"' | select-object -Property Name | Export-Csv (Join-Path $Export_Path_External -ChildPath "Active_Directory_Clients.csv") -NoTypeInformation
}

CodePudding user response:

Your immediate problem is that the variable $Export_Path_External does not exist in the remote session only your local session. To use it you need to reference it as $using:Export_Path_External However you will then likely run in to a kerberos double hop issue when making a connection back to your computer. See the suggestions others have made in the comments.

  • Related