Author Posts

July 31, 2018 at 3:33 pm

My goal is to remotely make 2 quick files containing mappings for mapped drives and network printer connections and place the files in the remote c:\temp folder.

When i run the code it applies everything to my own pc. However, if i type each line individually, it works perfect.

side note: I noticed that i had to use the userid and password of the person that was currently logged in to the remote system to get to the point i'm currently at.

$userid= Read-Host 'Enter Userid'
$pcname= read-host 'Enter PC Name'
Enter-PSSession -ComputerName $pcname -Credential domain\$userid 
Get-ChildItem -Path HKCU:\Network | Out-File c:\temp\Drives.txt -Force
Get-ChildItem -Path HKCU:\Printers\Connections | Out-File c:\temp\printers.txt -Force
Exit-PSSession

Any ideas why i cant run this all at once?

Thanks again!

-michael

July 31, 2018 at 3:35 pm

Whoops. Had the /pre the wrong way.

July 31, 2018 at 6:09 pm

This won't work if your goal is to run it all at once, instead of one line at a time. Read-host doesn't work that way. Saving Read-host to $userid and $pcname will not prompt for input when those variables are used as parameters. Check out the help on Read-host.

The best approach is to create a function with mandatory parameters called $userid and $pcname that accept string data. For example:

Function Create-files {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true)]
            [string]$userid,
            [parameter(Mandatory=$true)]
            [string]$pcname
        )
        
Enter-PSSession -ComputerName $pcname -Credential domain\$userid 
Get-ChildItem -Path HKCU:\Network | Out-File c:\temp\Drives.txt -Force
Get-ChildItem -Path HKCU:\Printers\Connections | Out-File c:\temp\printers.txt -Force

}

Create-files

July 31, 2018 at 6:28 pm

Thanks for the help Richard.

I'm only a few handful of chapters in to my 1st powershell book. The "function" part is new to me. I will look this over and see what i can learn.

Thanks again!

-michael