Unable to use Copy-Item in an icm command

Hi Scripting guru’s

I just completed Microsoft MVA “Getting started with Microsoft Powershell” in other words I’m totally green to Powershell scripting.

I want to make the script start a remote session to listed serveres in the $servers list, restart all services starting with “MicrosoftDynamicsNavServer$NAS”. When its complete it will look in the eventlog under application for instance ID 214 and write the 10 newest events to a HTM-fil and copy it to an IIS. It restart the services and create the HTM-file but I’m not able to copy the index.htm file to a webserver.

I get error’s like access denied and try without Credential. If I try to run Copy-Item C:\temp\index.htm -destination \WebServer\NAV$ on my local machine i works fine.

    $cred = Get-Credential
    $Servers = 'NAV-Serv01', 'NAV-Serv02'

    $WebServer = 'WebServer'

    $Session = New-PSSession -ComputerName $Servers -Credential $cred -Name $Servers
    $WebServerSession = New-PSSession -ComputerName $WebServer -Credential $cred
    icm -Session $Session {Restart-Service 'MicrosoftDynamicsNavServer$NAS*'}
    icm -Session $Session {Get-EventLog -LogName Application -InstanceId 214 -Newest 10 -Verbose | ConvertTo-Html -As Table | Out-File C:\temp\index.htm}

#Copy-Item -Path C:\temp\index.htm -Destination \\webserver\inetpub\wwwroot\NAV
Copy-Item -Path C:\temp\index.htm -Destination C:\inetpub\wwwroot\NAV -ToSession $WebServer

That’s because of the difference in how SMB (using a UNC) and Remoting handle credentials. You need to ensure you’re providing the session with a credential that has write access to the destination folder.

I found out there where no need to ICM, so I changed the script.

$Servers = 'NAV-Serv01', 'NAV-Serv02', 'NAV-Serv03', 'NAV-Serv04'
$WebServer = 'WEB'

$Servers |
	ForEach-Object{
		Get-Service 'MicrosoftDynamicsNavServer$NAS*' -ComputerName $_| Restart-Service
		Get-WinEvent -FilterHashtable @{LogName='Application';Id=214} -MaxEvents 20 -ComputerName  $_
	} |
        Select-Object MachineName, ID, Message |
	ConvertTo-Html |
	Out-File C:\temp\index.htm
Copy-Item C:\temp\index.htm -Destination \\$WebServer\c$\inetpub\wwwroot\NAV

Moving to correct forum.