what am i missing?

Welcome Forums General PowerShell Q&A what am i missing?

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #105652

    Participant
    Points: 0
    Rank: Member

    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
  • #105655

    Participant
    Points: 0
    Rank: Member

    Whoops. Had the /pre the wrong way.

  • #105674

    Participant
    Points: 7
    Rank: Member

    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
    
  • #105677

    Participant
    Points: 0
    Rank: Member

    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

The topic ‘what am i missing?’ is closed to new replies.