I have been working on a script that can remove an old version of zabbix from our servers, and install the latest version.
I am having a problem copying a file from a shared folder when I am using invoke-command.
I can run this script directly from the target machine and I can copy the folder perfectly:
$Installer = '\\Server01\Zabbix'
Write-Host -f green "[INFO]: Attempting to copy the folder located on $installer."
try{
Copy-Item -Path $installer -Destination c:\ -recurse -force
Write-Host -f green "[INFO]: $installer is succesfully copied on C:\Zabbix on $env:computername." }
catch
{write-host -f red "[ERROR]: Copying $installer encountered an error on $env:computername : $error"
$error.clear()}
Since I will be running this on multiple servers, I chose the method:
Invoke-Command -ComputerName $server -ScriptBlock{}
here is a sample code:
$servers = @( 'Server02')
foreach ($server in $servers) {
Invoke-Command -ComputerName $server -ScriptBlock {
$Installer = '\\Server01\Zabbix'
Write-Host -f green "[INFO]: Attempting to copy the folder located on $installer."
try{
Copy-Item -Path $installer -Destination c:\ -recurse -force
Write-Host -f green "[INFO]: $installer is succesfully copied on C:\Zabbix on $env:computername." }
catch
{write-host -f red "[ERROR]: Copying $installer encountered an error on $env:computername : $error"
$error.clear()}
}}
and below is the error message:
[INFO]: Attempting to copy the folder located on \\Server01\Zabbix.
Access is denied
+ CategoryInfo : PermissionDenied: (\\Server01\Zabbix:String) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName : Server02
Cannot find path '\\Server01\Zabbix' because it does not exist.
+ CategoryInfo : ObjectNotFound: (\\Server01\Zabbix:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName : Server02
I am using the same accounts, when accessing server 02 directly and running the commands without invoke-command. Access should be fine.
can anyone help?
thanks!