?
Solved

what is wrong with this powershell code?

Posted on 2010-09-15
7
Medium Priority
?
385 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
Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

 

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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A brief introduction to what I consider to be the best editor for PowerShell.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

771 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