Solved

what is wrong with this powershell code?

Posted on 2010-09-15
7
378 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 16

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

15 Experts available now in Live!

Get 1:1 Help Now