locked
ICM error when using Copy-Item RRS feed

  • Question

  • Hi Scripting Guys

    I just completed Microsoft Virtual Academy "Getting started with microsoft power shell". In other words I'm as green as I can be.

    I want to create a script which restart 5 NAV services on every servers in the $servers list, when its done it generate a HTM-file and pull the newest 20 events from the applications log and write it to the index.htm file. When the file have been created it should copy the file to an IIS server ($WebServer).

    My script do the restart and HTM generate part fine, but I can't get it to copy the file to a remote server. I get errors like access denied and "Invoke-Command : Cannot bind parameter 'Session'. Cannot convert the "NAV-TestServ01" value of type "System.String" to type "System.Management.Automation.Runspaces.PSSession"?

        $cred = Get-Credential
        $Servers = 'NAV-Serv01', 'NAV-Serv02', 'NAV-Serv03', 'NAV-Serv04'
    
        $WebServer = 'WEB'
    
        $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 20 -Verbose | ConvertTo-Html -As Table | Out-File C:\temp\index.htm}
    
        icm -Session $Servers {Copy-Item C:\temp\index.htm -Destination C:\inetpub\wwwroot\NAV -ToSession $WebServer}

    Thursday, September 7, 2017 2:08 PM

Answers

  • You do not need remote sessions to do this:

    $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


    \_(ツ)_/



    • Edited by jrv Thursday, September 7, 2017 4:21 PM
    • Marked as answer by BrianLind Friday, September 8, 2017 7:55 AM
    Thursday, September 7, 2017 4:17 PM
  • Thank you very much jrv it worked like a charm.

    How can I see what select-object there is to choice from?

    help select-object -full


    \_(ツ)_/

    • Marked as answer by BrianLind Friday, October 27, 2017 11:27 AM
    Friday, September 8, 2017 1:51 PM

All replies

  • You do not need remote sessions to do this:

    $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


    \_(ツ)_/



    • Edited by jrv Thursday, September 7, 2017 4:21 PM
    • Marked as answer by BrianLind Friday, September 8, 2017 7:55 AM
    Thursday, September 7, 2017 4:17 PM
  • Thank you very much jrv it worked like a charm.

    How can I see what select-object there is to choice from?

    Friday, September 8, 2017 1:49 PM
  • Thank you very much jrv it worked like a charm.

    How can I see what select-object there is to choice from?

    help select-object -full


    \_(ツ)_/

    • Marked as answer by BrianLind Friday, October 27, 2017 11:27 AM
    Friday, September 8, 2017 1:51 PM