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

x
?
Solved

Creating Folder in Remote Servers Using Powershell

Posted on 2011-09-22
10
Medium Priority
?
5,374 Views
Last Modified: 2012-05-12
I want to create a folder in list of servers that i specify in a .csv file. The format of csv file is

server                               user      password
192.168.14.15      sa      password1

I get the following error when i execute the below code from local machine where i have installed Powershell v2.

Powershell has not been installed on the server where i need to create a folder.
 Please provide me a solution.

Cannot convert value "\\@{server=192.168.15.14; user=sa; password=password1}.server\root\cimv2:Win32_Process" to type
"System.Management.ManagementClass". Error: "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"
At line:4 char:16
+ $p = [WMIClass] <<<< "\\$computer\root\cimv2:Win32_Process"
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException.

If i execute the same code for local machine i get the same error but i get the folder created.

$Path="D:\Test1"
$servers = Import-csv "C:\Powershell\serverlist.csv"
ForEach ($server in $servers)
{
$computer = "$server.server"
$p = [WMIClass]"\\$computer\root\cimv2:Win32_Process"
$p.Create("cmd.exe /c md $Path")
}

Open in new window

0
Comment
Question by:SQLM_M
[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
  • 6
  • 4
10 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 2000 total points
ID: 36580114
Depending on your header in the CSV file you could do something like this. In this example the header of the servers would be servername and also assumes the user running the script has access to the D$ admin share.




$servers = Import-csv "C:\Powershell\serverlist.csv"
ForEach ($server in $servers){New-Item "\\$($Server.Servername)\d$\Test1" -type directory}
0
 

Author Comment

by:SQLM_M
ID: 36580325
Hi,

I get the following error when i execute this code

$servers = Import-csv "C:\Powershell\serverlist.csv"
ForEach ($server in $servers){New-Item "\\$($Server.Servername)\d$\Test1" -type directory}


New-Item : The path is not of a legal form.
At line:1 char:39
+ ForEach ($server in $servers){New-Item <<<<  "\\$($server.servername)\d$\Test1" -type directory}
    + CategoryInfo          : InvalidArgument: (\\\d$\Test1:String) [New-Item], ArgumentException
    + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.NewItemCommand
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36580422
in your CSV file is the header on the column for the servers "ServerName". So it should look like this

Servername
Server1
Server2
Server3
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:SQLM_M
ID: 36580531
Hi,

I changed the csv header but still i get the login failure error.

New-Item : Logon failure: unknown user name or bad password.
At line:1 char:39
+ ForEach ($server in $servers){New-Item <<<<  "\\$($server.servername)\d$\Test1" -type directory}
    + CategoryInfo          : WriteError: (\\192.168.151.38\d$\Test1:String) [New-Item], IOException
    + FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36580633
The account you are using does not have access to the remote server.

Are you able to access
\\192.168.151.38\d$
0
 

Author Comment

by:SQLM_M
ID: 36580716
It prompts for login credentials. I could able to access if i give the credentials...
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36580810
try this

{New-Item "\\$($Server.Servername)\d$\Test1" -type directory -credentials DOMAIN\USERNAME}
0
 

Author Comment

by:SQLM_M
ID: 36584902
Thanks for your solution.
0
 

Author Comment

by:SQLM_M
ID: 36584919
I've requested that this question be closed as follows:

Accepted answer: 0 points for SQLM_M's comment http:/Q_27321945.html#36580325

for the following reason:

Thanks for your solution.
0
 

Author Comment

by:SQLM_M
ID: 36584920
Sorry i have wrongly chosen the solution. The exact solution is provided by KenMcF solution id is 36580114
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

A brief introduction to what I consider to be the best editor for PowerShell.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

609 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