Unable to use Copy-Item in an icm command

Welcome Forums General PowerShell Q&A Unable to use Copy-Item in an icm command

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 5 months ago.

  • Author
  • #78967

    Topics: 2
    Replies: 1
    Points: 11
    Rank: Member

    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
  • #79253

    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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.

  • #79274

    Topics: 2
    Replies: 1
    Points: 11
    Rank: Member

    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 |
    		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
  • #85217

    Topics: 23
    Replies: 140
    Points: 30
    Rank: Member

    Moving to correct forum.

The topic ‘Unable to use Copy-Item in an icm command’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort