Home > database >  can I use Out-File with a variable containing a UNC path to write a txt file?
can I use Out-File with a variable containing a UNC path to write a txt file?

Time:03-29

My goal is to allow a user to input a location using the below code and have the script results written to a txt file in their desired network file share location. Our currently deployed PowerShell version 5.1.19041.1320

Write-Host "Where would you like the results output to?"
$OutPutLocation =Read-Host -Prompt "Output location"
Invoke-Command -ComputerName $Remoteservername {Get-WinEvent  -FilterHashtable @{logname = 'System'; id = 1074} -MaxEvents 5 | Out-String -Width 500 | Out-File $OutPutLocation  -Verbose}

The variable is capturing the data, like in this example.

Where would you like the results output to?
Output location: \\servername\folder1\folder2\RCA.txt

PS C:\Users\TestUser\> $OutPutLocation
\\servername\folder1\folder2\RCA.txt

Without doubt, this is an authoring error because I don't know how to utilize the location captured in $OutPutLocation.

If the use of a variable in this manner is not supported, please educate me as to why and feel free to provide any guidance that will allow me to correctly capture and use the user's input. Please use the simplest resolution examples, so as a novice user, I can understand the command's working process. Thank you for your time and any assistance you provide.

I know if you remove the variable and use a local path like the line below, if functions as expected.

Get-WinEvent -FilterHashtable @{logname = 'System'; id = 1074} -MaxEvents 5 | Out-String -Width 500 | Out-File C:\Temp\short-clean-reboots.txt -Verbose}

CodePudding user response:

If you need to output to a remote share from a remote host you're very likely to encounter a double-hop issue:

  1. You are logged in to ServerA.
  2. From ServerA, you start a remote PowerShell session to connect to ServerB.
  3. A command you run on ServerB via your PowerShell Remoting session attempts to access a resource on ServerC.
  4. Access to the resource on ServerC is denied, because the credentials you used to create the PowerShell Remoting session are not passed from ServerB to ServerC.

Doug's helpful comment and the nice linked article, Kerberos Double Hop, as well as Making the second hop, which speaks specifically from a PowerShell remoting standpoint, provide alternatives around this, such us Service Principal Name and Kerberos Delegation or CredSSP among others.

The recommended approach in this case would be to avoid all this trouble by capturing the remote output to your local session to then output to the UNC path from your local session:

Write-Host "Where would you like the results output to?"
$OutPutLocation = Read-Host -Prompt "Output location"
if(-not (Test-Path $OutPutLocation)) {
    throw "Unable to reach $OutPutLocation"
}
$remoteLogs = Invoke-Command -ComputerName $Remoteservername {
    Get-WinEvent -FilterHashtable @{logname = 'System'; id = 1074} -MaxEvents 5
}

# Much better to export an object as CSV
$remoteLogs | Select-Object TimeCreated, Id, LevelDisplayName, Message |
    Export-Csv $OutPutLocation -NoTypeInformation

CodePudding user response:

After studying the feedback and reference material, I found the issue. I was using Enter-PSSession which is a "temporary session" and dose not pass commands the same way a New-PSSessions does ( at least the way I was using it ). Here is what I used to correct the issue and build out the rest of the script.

(code excerpt) Write-Host "What server are we working with?" $Remoteservername =Read-Host -Prompt "Server Name" $A = New-PSSession -Computername $Remoteservername

Invoke-Command -ComputerName $Remoteservername { Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime | Out-String -Width 500 | Out-File C:\Temp\ICC\lastboot.txt -Verbose ; }

Copy-Item -Path C:\Temp\ICC"*.txt" -Destination C:\Temp\ICC\ -FromSession $A}

Get-PSSession | Remove-PSSession

This avoids the double hop and lets me copy files to the local workstation. I know it's not the most elegant code but this is my very first powershell script. As suggested I will set the output to a csv file now that the script works.

I would like to thank you all for taking time to provide your professional knowledge and guidance. It's very exciting to have a place where such dedicated professionals extend support models beyond the prescribed limits.

  • Related