2

Requirement: I am trying to create the BKP folder in 10 server using the below code but some how it is not creating the new folder in servers.

foreach ($instance in Get-Content "C:\servers\List.txt")
{
    $local = Get-Location;
    $final_local = "C:\BKP";

    if (!$local.Equals("C:\"))
    {
        cd "C:\";
        if ((Test-Path $final_local) -eq 0)
        {
            mkdir $final_local;
            cd $final_local;
        }

        ## if path already exists
        ## DB Connect
    }
}
3
  • mkdir for make new Folder? Commented Oct 19, 2016 at 5:56
  • Hi Arashzag , thanks for reply, Already I used mkdir in my code.can you try to share the code which can help me. Commented Oct 19, 2016 at 5:57
  • do you want to make new folder with $final_local path ? Commented Oct 19, 2016 at 5:59

3 Answers 3

2

First : you are using the wrong test, you should use :

if($local.Path -eq "C:\")

The equal method is there to compare objects have a look to :

$local | get-member
$local | fl *

Be careful PowerShell CmdLet return objects.

Second : if $local.Path is equal to "C:\", you create nothing.


Your code would look like :

ForEach ($instance in $(Get-Content "C:\servers\List.txt")) 
{ 
  $local = Get-Location;
  $final_local = "C:\BKP";

  if($local.Path -ne "C:\")
  {
    cd "C:\";
    if((Test-Path $final_local) -eq 0)
    {
      mkdir $final_local;
      cd $final_local;
    }

    ## if path already exists
    ## DB Connect
}
Sign up to request clarification or add additional context in comments.

6 Comments

Its creating folder in Local instead of remote servers specified in the c:\server\list.txt
Your test is not the good one .Equals do not do what you expect.
Thanks JPBlanc for your reply. I didn't understand. Please change my code .
I do not try to understand the context, i just change your test (the if clause).
@Franklin - you are looping over the contents of a serverlist but you are nowhere connecting to that server *(by share or remoting) to create the folder.
|
0

use this command to create directory :

ForEach ($instance in Get-Content "C:\servers\List.txt") 

{ 
$local = Get-Location;
$final_local = "C:\BKP";

if(!$local.Equals("C:\"))
{
    cd "C:\";
    if((Test-Path $final_local) -eq 0)
    {
        New-Item -ItemType Directory -Force -Path "$final_local";
        cd $final_local;

    }

    ## if path already exists
    ## DB Connect

}

} 

3 Comments

I didn't find any difference for the script I posted and the above code.
still I can see the folder is created in local only
Can some one help me on this Topic
0

Try using the invoke-command i am assuming credentials are needed for the servers you can skip it if it works for you.

$credentials = Get-Credential

$create = { $local = Get-Location;
            $final_local = "C:\BKP"

             if(!$local.Equals("C:\")){
                cd "C:\";
                 if((Test-Path $final_local) -eq 0){
                      New-Item -ItemType Directory -Force -Path "$final_local";
                      cd $final_local;
               }
         ## if path already exists
           ## DB Connect
     }
 }
ForEach ($instance in Get-Content "C:\servers\List.txt")  {
Invoke-Command -ScriptBlock $create -ComputerName $instance -Credential $credentials
}

1 Comment

Thansk Ashu for your help but unfortunately it is not working.I mean its creating on local drive only.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.