Solved

Creating Folder in Remote Servers Using Powershell

Posted on 2011-09-22
10
4,721 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
  • 6
  • 4
10 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
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…

680 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