Solved

Creating Folder in Remote Servers Using Powershell

Posted on 2011-09-22
10
4,203 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article will help you understand what HashTables are and how to use them in PowerShell.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now