Solved

what is wrong with this powershell code?

Posted on 2010-09-15
7
379 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now