Return data from remote PC connection

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
bsmith1965
Posts: 5
Last visit: Tue Sep 14, 2021 7:22 am

Return data from remote PC connection

Post by bsmith1965 »

So I am writing a script to run on users machine that do not have AD or quest modules for AD lookup.

The clients are Windows 7 so I have Powershell 2.0 on the domain PC's which I cannot upgrade.

From the clients PC I will be running a command to connect to a server as particular user with elevated rights

So it will be as follows

Invoke-command -cred $cred -ComputerName $Server -scriptblock { Get-aduser $Domain_User -Properties Extensionattribute1, ExtensionAttribute2}

I need to keep the values in the extensionAttributes for later use

Is this achievable ?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Return data from remote PC connection

Post by jvierra »

What do you mean by "keep". Are u0i asking how to save these in a file?
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Return data from remote PC connection

Post by dan.potter »

You are trying to use the AD cmdlets against machines that don't have the AD module? :?
User avatar
bsmith1965
Posts: 5
Last visit: Tue Sep 14, 2021 7:22 am

Re: Return data from remote PC connection

Post by bsmith1965 »

To make this simpler I need to get 2 extension attributes , I have to have 1 returned in a variable and the other in another variable.

The PC's where this script will eventually run has PS 2.0 and nothing else so I need to remote a server and get the attributes here, Here is my script

$Gloabal:samid = $env:USERNAME # Get SamAccountName
$UAdmin = 'Service@domain.net # Service account with necessary rights
$Password = Get-Content C:\Support\UP.txt | convertto-securestring # Get previously save password
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $UAdmin, $Password # Crete credential Object
$Server = "Server.domain.net" # FQDN of the Server with AD CMDlets
$ext1 = Invoke-Command -credential $Credential -ComputerName $Server -ScriptBlock { Get-ADUser $samid -Properties ExtensionAttribute1 } # Get ext1
$ext2 = Invoke-Command -credential $Credential -ComputerName $Server -ScriptBlock { Get-ADUser $samid -Properties ExtensionAttribute2 } # Get ext2

I plan to set these ext1 and 2 properties later once I do some validation.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Return data from remote PC connection

Post by jvierra »

Here is how we would do this in PowerShell:
  1. # never save passwords in a text file.  You might as well post the password on Twitter
  2. $Credential = Get-Credential Service@domain.net
  3. $sb = { Get-ADUser $args[0] -Properties ExtensionAttribute1, ExtensionAttribute2 }
  4. $user = Invoke-Command -ScriptBlock $sb -credential $Credential -ComputerName 'Server.domain.net' -ArgumentList $env:USERNAME
  5. $user.ExtensionAttribute1
  6. $user.ExtensionAttribute2
This topic is 7 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked