?
Solved

Creating Folder in Remote Servers Using Powershell

Posted on 2011-09-22
10
Medium Priority
?
5,519 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 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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).
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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 anti-spam), the admin…
Screencast - Getting to Know the Pipeline

830 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