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

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


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
  }
Advertisements
Categories: Powershell Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: