Solved

what is wrong with this powershell code?

Posted on 2010-09-15
7
382 Views
Last Modified: 2012-08-14
I have created the following script which doesnt seem to work and i cant figure out why.
 
Import-Module PSRemoteRegistry
$machines = get-content "C:\Users\noulis\Desktop\User.csv"
foreach($computername in $machines)
{
  $os = gwmi win32_operatingsystem -computername $computername
  $os.description = $user
  $os.put()
}
All i need it to do is read a csv file ( which has two columns ,one for a computer name and one for the username ) and then apply the username to the actual computer desciption field. Any suggestions welcome
 Thanks in advance
0
Comment
Question by:MOSADMIN
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:Willy Van den Houten
ID: 33680110
What is the error you got ?  Mostly the error explains the solution...
0
 

Author Comment

by:MOSADMIN
ID: 33680207
Sorry for forgetting to insert the error. I have attached it below :

error.JPG
0
 
LVL 3

Expert Comment

by:Willy Van den Houten
ID: 33680273
first one - RPC unavailable means that the computername doesn't exist or isn't recognized
user doesn't exist too

Can you check your code with the real computername and not with a script ?

Normally you should have the same error.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:MOSADMIN
ID: 33680344
what do you mean by checking the code with the real computer name ?  something else i have done that works is this :
$os = gwmi win32_operatingsystem -computername W7E728   ( this is a computer name)
$os.description = "Nick's Computer"
$os.put()

If it comes to it i can sit and run this 'script' for every single computer we have( 150 which will take quite a while). If the error is because it cant find the computername(i am sure they are right) could there be a problem in the way its importing the csv file?  
0
 
LVL 3

Expert Comment

by:Willy Van den Houten
ID: 33680694
I guess so... or maybe the rights ?

If you enter a wrong computername (just for testing - you will see the same RPC error, that's why I sugested there was a problem with the computer names)

0
 
LVL 17

Accepted Solution

by:
Learnctx earned 500 total points
ID: 33680901
The problem is you're not connecting to the workstation. I would say the reason being how you're importing the CSV file. From that point forward all the other parts of your script will fail as no object exists to interact with. Your CSV file has 2 columns, is there a header row? I'm assuming probably not and it's just:

computer1,user1
computer2,user2

If that's the case try this.

If you have  a header row, use import-csv. But I would say from what is happening your command is coming out as:

$os = gwmi win32_operatingsystem -computername "computername,username"

Which is why you are unable to connect to WMI.
$machines = get-content "C:\Users\noulis\Desktop\User.csv"
foreach($entry in $machines)
{
  # assuming computer name is the first column, user name is second
  $computername = $entry.split(",")[0]
  $user = $entry.split(",")[1]
  $os = gwmi win32_operatingsystem -computername $computername
  $os.description = $user
  $os.put() 
}

Open in new window

0
 

Author Closing Comment

by:MOSADMIN
ID: 33681142
You are a genius! thanks a lot
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question