Archive

Archive for March, 2012

[Powershell] Calling NET USE to create shared folders on remote server using Powershell

March 1, 2012 Leave a comment

Below is the powershell script sample on how to call NET USE command to create a shared folder on any remote server.


$netShareSettings+= @{	"Server"="TargetServer";
                         "Path"="E:\MyFolderToBeShared";
			 "ShareName" = "SharedFolder";
			  "UserAndPermission" = ("Domain\user1;FULL","Domain\User2;FULL");
		      }

foreach($netShareSetting in $netShareSettings)
  {

   $script =
     {

	$userParam = "";

	foreach($pm in $args[0].UserAndPermission)
	{
		$arr = $pm.Split(";");
		$userName = $arr[0];
		$perm = $arr[1];
		$userParam+= "/grant:`"{0}`,{1}`" " -f $userName, $perm
	}

	$command = "net share {0}=`"{1}`" {2}" -f $args[0].ShareName,$args[0].Path, $userParam
	$msg = " Executing Command: '{0}' on '{1}'" -f $command,$args[0].Server
	$msg = Invoke-Expression $command | Out-String
	$msg = "  Results: " + $msg.Trim();
	Write-Host $msg

     }

     Invoke-Command -ScriptBlock $script -ComputerName $netShareSetting.Server -ArgumentList $netShareSetting
  }
Categories: Powershell Tags: ,
%d bloggers like this: