Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

what is wrong with this powershell code?

Posted on 2010-09-15
7
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist 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 18

Accepted Solution

by:
Learnctx earned 2000 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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