简体   繁体   中英

Pass runtime parameters in PowerShell Script

I have a below powerShell script that creates homedrive for user,

Import-Module ActiveDirectory 2>&1 | Write-Host; 
if($?)
{

$homeDir = "\\CORP.com\HOME\Jdoe";
$user = "jdoe";
$domain = "Corp";

New-Item "$homeDir" -type directory;

$acl = Get-Acl "$homeDir";
$permission = "$domain\$user","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow";
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission;

$acl.SetAccessRule($accessRule);

$acl | Set-Acl "$homeDir";
}

Values within $homeDir and $User will be passed on runtime basis.

How to execute above script along with pass runtime values in $homeDir and $User attribute.

I have tried to execute,

. 'C:\\hd.ps1' $homeDir = "\\\\CORP.com\\HOME\\test" $user = "test" ; without success.

Can anyone guide, what i am doing incorrect.

Put

param(
$homeDir,
$user
)

At the top of the script and call using

Powershell -File "C:\hd.ps1" -homeDir "\\CORP.com\HOME\test" -user "test" 

Why are you doing this?

Import-Module ActiveDirectory 2>&1 | Write-Host; 

If you are on the DC doing this or if you have the RSAT tools on your workstation, if you are on PowerShell v3+ or higher, this gets auto loaded the moment you use an AD cmdlet.

Also never user Write-Host for anything that you plan to need later. It empties / clears the buffer. Write-Host is only good for text coloring or other formatting needs in s

Make this a collection from a file for example and just read it in. I'm just using a list here:

$UserFile = @'
Property,Value
homeDir,\\CORP.com\HOME\Jdoe
user,jdoe
Targetdomain,Corp
'@ | ConvertFrom-Csv

# Results

Property     Value               
--------     -----               
homeDir      \\CORP.com\HOME\Jdoe
user         jdoe                
Targetdomain Corp 

If you are doing this from a remote machine, then you cannot use local varibles in a remote session unless you set its scope.

Get-Help about_remote_variables -Full

About Remote Variables

LONG DESCRIPTION

You can use variables in commands that you run on remote computers.Simply assign a value to the variable and then use the variable inplace of the value.

By default, the variables in remote commands are assumed to be definedin the session in which the command runs. You can also use variablesthat are defined in the local session, but you must identify them aslocal variables in the command.

USING LOCAL VARIABLES

You can also use local variables in remote commands, but you mustindicate that the variable is defined in the local session.

Beginning in Windows PowerShell 3.0, you can use the Using scopemodifier to identify a local variable in a remote command.

The semi-colons are not needed in PowerShell, unless the items are on the same line.

You cannot call this code this way...

'C:\hd.ps1' $homeDir = "\\CORP.com\HOME\test" $user = "test"

... since you did not specify any params in your code.

So, something like this...

Note: I am not in a position to test this... please do only in a test environment So this is off the cuff...

ForEach($UserLine in $UserFile)
{
    New-Item $UserLine.homeDir -type directory

    $acl = Get-Acl $UserLine.homeDir
    $permission = ($Using:UserLine.Targetdomain + '\' + $Using:UserLine.user),'FullControl', 'ContainerInherit, ObjectInherit', 'None', 'Allow'
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission

    $acl.SetAccessRule($accessRule)

    $acl | Set-Acl $Using:UserLine.homeDir
}

If you want this to be a parameterized function, then this.,.

Function New-ADUserHomeDirSettings
{
    [cmdletbinding()]

    Param
    (
        [string]$homeDir,
        [string]$user,
        [string]$Targetdomain
    )

    $acl = Get-Acl $UserLine.homeDir
    $permission = ($Using:UserLine.Targetdomain + '\' + $Using:UserLine.user),'FullControl', 'ContainerInherit, ObjectInherit', 'None', 'Allow'
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission

    $acl.SetAccessRule($accessRule)

    $acl | Set-Acl $Using:UserLine.homeDir
}

New-ADUserHomeDirSettings -homeDir '' -user '' -Targetdomain ''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM