Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PowerShell
Hi,
I am unable to execute below query in my power-shell console. This PC is under a domain. But this is working fine in systems which are not in domain.
$server='host_name';$pwd= convertto-securestring 'password' -asplaintext -force;$cred=new-object  -typename System.Management.Automation.PSCredential -argumentlist 'domainname\username',$pwd; invoke-command -computername $server -credential $cred -scriptblock {Get-WmiObject -namespace "root\cimv2" -query 'select CSName from Win32_OperatingSystem'}
 
Following is the sample Power-Shell query, which I tried.
 
$server='host_name';$pwd= convertto-securestring 'password' -asplaintext -force;$cred=new-object  -typename System.Management.Automation.PSCredential -argumentlist 'domainname\username',$pwd;Get-EventLog -ComputerName $Server -LogName Application 

Can anyone shed some light into this issue.
 
Thanks for your time.
Sebastian
Posted 21-Dec-12 1:46am
Edited 31-Dec-12 1:47am
v2
Comments
Zoltán Zörgő at 21-Dec-12 8:30am
   
Is the source machine (the one you are running the script on) part of the same domain as the target machine? Why are you doing this in such complicated way? Get-WmiObject can be run by its own against remote machines. Check documentation: http://technet.microsoft.com/en-us/library/hh849824.aspx
Sebastian T Xavier at 27-Dec-12 1:48am
   
Hi, I tried the way you suggested and it worked in WMI. But I want to execute queries which are purely Power-Shell. My source and target machine are in same domain. Following is the sample Power-Shell query, which I tried. $server='host_name';$pwd= convertto-securestring 'password' -asplaintext -force;$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist 'domainname\username',$pwd;Get-EventLog -ComputerName $Server -LogName Application Thanks for your time. Sebastian
notjustme at 10-Mar-13 8:05am
   
Get-Eventlog never even gets to use the credentials you've specified. If you don't have the rights to access the eventlog with the current user you could run PowerShell as one that has. If it's used in like a scheduled job you could run it as the desired user there, just make sure that the particular service account has 'log on as batch job' rights. Storing passwords in plain text in a file gives me the hives but here's another option that might work for ya if you have remoting (Get-Help about_remote) enabled (code hasn't been tested but should be in the ballpark); $server='host_name' # Since $pwd is one of PowerShell's automatic variables I named it $passwd instead. $passwd= convertto-securestring 'password' -asplaintext -force $cred=new-object -typename System.Management.Automation.PSCredential -argumentlist 'domainname\username',$passwd Invoke-Command -ComputerName $server -Credential $cred -Scriptblock { Get-Eventlog -LogName Application }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 756
1 OriginalGriff 452
2 Tadit Dash 350
3 sanket saxena 329
4 Peter Leow 228
0 Sergey Alexandrovich Kryukov 11,890
1 OriginalGriff 7,335
2 Peter Leow 5,003
3 Abhinav S 3,943
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid