I’m trying to run a script which calls a .exe file on a number of servers in our AD and I’m not getting it to work as I intended.
The original script looks like this:
#Requires -version 5.1
[CmdLetBinding()]
Param ()
Try {
$computer = $env:computername
$dt = Get-Date -Format "yyyy/MM/dd HH:mm:ss"
$disks = Get-CimInstance –Query "SELECT * FROM win32_logicaldisk WHERE DriveType = '3'"
foreach($disk in $disks){
$log = \\[SERVERSHARE]\[TOOL].exe "$($disk.DeviceID)\"
[PSCustomObject]@{
Time = $dt
Server= $env:computername
Disk = $disk.DeviceID
Result = "$log"
} | Export-Csv -Path '\\[SERVERSHARE]\[LOG].csv' -NoClobber -Encoding UTF8 -Append -Delimiter ";" -NoTypeInformation
Write-Host "$computer $($disk.DeviceID) was scanned"
}
}
Catch {
Write-Host -BackgroundColor Red "Error: $($_.Exception)"
Break
}
And it works if I copy the script to the remote server and logs in via RDP and runs it manually.
I tried to modify the script like this:
#Requires -version 5.1
[CmdLetBinding()]
Param ()
$Server = '[SERVER]'
Try {
Invoke-Command -ComputerName $Server -ScriptBlock {
$computer = $env:computername
$dt = Get-Date -Format "yyyy/MM/dd HH:mm:ss"
$disks = Get-CimInstance –Query "SELECT * FROM win32_logicaldisk WHERE DriveType = '3'"
foreach($disk in $disks){
$log = Start-Process -FilePath '\\[SERVERSHARE]\[TOOL].exe' -Argumentlist "$($disk.DeviceID)\"
[PSCustomObject]@{
Time = $dt
Server= $env:computername
Disk = $disk.DeviceID
Result = "$log"
} | Export-Csv -Path '\\[SERVERSHARE]\[LOG].csv' -NoClobber -Encoding UTF8 -Append -Delimiter ";" -NoTypeInformation
Write-Host "$computer $($disk.DeviceID) was scanned"
}
}
}
Catch {
Write-Host -BackgroundColor Red "Error: $($_.Exception)"
Break
}
My thinking was that it should run the entire scriptblock from the remote server but I get two access denied permission errors. I’m guessing the first one is for trying to access the .exe file, but it doesn’t say specifically but the other one is specific about not being able to open the log file:
Access is denied
+ FullyQualifiedErrorId : System.UnauthorizedAccessException
+ PSComputerName : [SERVER]
Access to the path '\\[SERVERSHARE]\[LOG].csv' is denied.
+ CategoryInfo : OpenError: (:) [Export-Csv], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
+ PSComputerName : [SERVER]
The share is (at the moment) entirely open, so it should be no problem accessing it.
Since the modification didn’t work as I had intended I tried to do this instead:
Invoke-Command -ComputerName [SERVER] -FilePath [ORGSCRIPT]
My understanding is that it should copy the script to the remote computer and run it there, but no go!
$S = New-PSSession -ComputerName [SERVER]
Invoke-Command -Session $S -FilePath [ORGSCRIPT]
Fails as well.
I also tried
Copy-Item [ORGSCRIPT] -Destination \\[SERVER]\C$
Invoke-Command -ComputerName [SERVER] -Command { C:\[ORGSCRIPT] }
There was no problem establishing a session or copying a file, but whenever I tried to actually run it I ended up with a permission denied error message something like this:
Error: System.UnauthorizedAccessException: Access is denied ---> System.ComponentModel.Win32Exception: Access is denied
--- End of inner exception stack trace ---
at System.Management.Automation.Utils.NativeFileExists(String path) ...
There’s more but you get the gist.
Am I thinking about this all wrong or should I be trying something else entirely?