Solved

Creating Folder in Remote Servers Using Powershell

Posted on 2011-09-22
10
4,963 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 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

691 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