Savill's FAQs: How PowerShell accesses remote files

Three times a week, John Savill tackles your most pressing IT questions. Today, he explains three PowerShell items: accessing remote files, suppressing command output, and sending multiple lines for a script block.

John Savill

December 2, 2017

2 Min Read
John Savill'ss FAQs on IT Pro Today Hero

Three times a week (Monday/Wednesday/Friday), John Savill tackles your most pressing IT questions.

Read through the FAQ archives, or send him your questions via email.

Today: Three PowerShell items about accessing remote files, suppressing command output, and sending multiple lines for a script block.

Q. I'm trying to access a file share from a remote PowerShell session but it's not working?

A.  This is the double-hop problem where you are trying to use your credential on a remote session and then go to a remote machine from within the remote session. This requires the use of CredSSP or constrained Kerberos delegation. Another approach is to pass a credential and map a drive to the remote file server you are trying to access.

In this example, focus on the parts in bold. You can see I pass the credential as an attribute to the code running remotely.

$NPSServers = "azuusenps1","azuusenps2","azuuswnps1","azuuswnps2","azueuenps1","azueuenps2","azuasenps1","azuasenps2"

$todayDate = Get-Date -Format MM-dd-yy
$ExportName = $todayDate + "-NPSConfig.xml"

$cred = Get-Credential

foreach($NPSServer in $NPSServers)
{
    write-output "Updating $NPSServer"
    Invoke-Command -ComputerName $NPSServer -ScriptBlock {
        if (-not (Test-Path 'c:NPS')) { New-Item -Path C:NPS -ItemType Directory }
        if (Test-Path C:NPS$($args[0])) {Remove-Item -Path C:NPS$($args[0]) -Force }
        Export-NpsConfiguration -Path C:NPS$($args[0])
        if (Test-Path C:NPSNPSConfig_output.xml) {Remove-Item -Path C:NPSNPSConfig_output.xml -Force }
        New-PSDrive -Name X -PSProvider FileSystem -Root \AZUUSWNPS2NPSGeneration -Credential $args[1] | out-null
        Copy-Item X:NPSConfig_output.xml C:NPS
        Remove-PSDrive X
        Import-NpsConfiguration -Path C:NPSNPSConfig_output.xml
    } -ArgumentList $ExportName, $cred
}


Q. How can I suppress output from commands in PowerShell?

A. The easiest way to suppress output is to send that output to null, e.g.

| out-null


Q. How can I send multiple lines for a script block with PowerShell?

A. It's actually very simple. Just put the lines on new lines within the curly brackets and it should work. For example:

Invoke-Command -ComputerName $NPSServer -ScriptBlock {
        if (-not (Test-Path 'c:NPS')) { New-Item -Path C:NPS -ItemType Directory }
        #if (Test-Path C:NPS$($args[0])) {Remove-Item -Path C:NPS$($args[0]) -Force }
        Export-NpsConfiguration -Path C:NPS$($args[0])
        if (Test-Path C:NPSNPSConfig_output.xml) {Remove-Item -Path C:NPSNPSConfig_output.xml -Force }
        New-PSDrive -Name X -PSProvider FileSystem -Root \AZUUSWNPS2NPSGeneration -Credential $args[1] | out-null
        Copy-Item X:NPSConfig_output.xml C:NPS
        Remove-PSDrive X
        Import-NpsConfiguration -Path C:NPSNPSConfig_output.xml
    } -ArgumentList $ExportName, $cred

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like