0

Working in PowerShell I need to export the App Pool information into a CSV and then export it to a different server on the network. When I run the following command:

Get-Website |
  select name, id, state, physicalpath,
         @{n="Bindings";e={($_.bindings | select -expa collection) -join ';'}} |
  Export-Csv -NoTypeInformation -Path \\prod-web5\e$\Hosted-1.csv

I receive the following Error:

Export-Csv : Access to the path '\\prod-web5\e$\Hosted-1.csv' is denied.
At line:3 char:11
+ Export-Csv <<<<  -NoTypeInformation -Path \\prod-web5\e$\Hosted-1.csv
+ CategoryInfo          : OpenError: (:) [Export-Csv], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

I can access the path through file explorer I have permissions to modify the files and write to the directory but I receive this error. From the research I have done the common solutions did not resolve the issue, Making sure the file name has extensions and etc.

I can use the C:\whatever and it will write to the C drive but I can not write to the other server.

I have also check with the Antivirus program because I noticed sometimes it will interfere with PowerShell. However, in this instance it is not affecting it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Brett
  • 161
  • 4
  • 16
  • e$ is an administrative share. Do you try to run tour script as administrator ? – JPBlanc Jan 07 '16 at 21:15
  • I am running it as administrator yes – Brett Jan 07 '16 at 21:28
  • 1
    I'm curious what would happen if you stored the stuff you want to export into a variable and then pass that into an `Invoke-Command` that runs locally on the remote (prod-web5) computer. Can you try this? `$stuff = Get-Website | select name, id, state, physicalpath, @{n="Bindings";e={($_.bindings | select -expa collection) -join ';'}} Invoke-Command -ComputerName "prod-web5" -ScriptBlock { param($stuff) $stuff | Export-Csv -NoTypeInformation -Path e:\Hosted-1.csv } -ArgumentList $stuff ` The only thing is you have make sure both computers are configured to allow this kind of remote execution. – Eric J. Price Jan 07 '16 at 21:43
  • Eric Thank you! That worked the only thing I had to add was the -credential – Brett Jan 07 '16 at 21:58
  • The only issue I ran into is looking at the CSV, it will only show one Site and there are 50+ Sites on the server – Brett Jan 07 '16 at 22:07
  • Interesting, the ArgumentList expects an array so it treats each website as a new variable and we just don't give the rest of them names in the param declaration. There is another post that addresses this, but essentially you wrap the array in an array so it will get picked up as an array for the variable population. – Eric J. Price Jan 07 '16 at 22:44

1 Answers1

3

Here you go...

$stuff = Get-Website | select name, id, state, physicalpath, @{n="Bindings";e={($_.bindings | select -expa collection) -join ';'}} 
Invoke-Command -ComputerName "prod-web5" -ScriptBlock { param($stuff) $stuff | Export-Csv -NoTypeInformation -Path e:\Hosted-1.csv } -ArgumentList (,$stuff)

This addresses your other concern about only getting one row instead of all of them which is addressed here.

Community
  • 1
  • 1
Eric J. Price
  • 2,740
  • 1
  • 15
  • 21