cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
dpadgett
Engaged Sweeper II
Add users to AD groups based on partial name

call with .\scriptname.ps1 -smartname {computer}



param (
[string]$smartname,
[Int32]$Seconds = 20,
[string]$Message = "waiting for 20 seconds to synchronise bits and pieces..please wait"

)

Import-Module ActiveDirectory


Function AddComputerToGroup{

$adgrp = Read-Host 'What is the AD group name? (Partial or full name)'
$adgrp = "*" + $adgrp + "*"
$adgrp2 = (Get-ADGroup -Filter {name -like $adgrp} | select Name,DistinguishedName,GroupCategory,GroupScope | Out-GridView -OutputMode multiple -Title 'Please select AD group name:').name

if ($adgrp2 -like $null) {
Write-Host "Please enter the AD group name:"
AddComputerToGroup
} elseif ($adgrp2 -ne $null) {
foreach ($adgrp3 in $adgrp2) {
Add-ADGroupMember -Identity "$adgrp3" -Members "$smartname$"
}
}
}
AddComputerToGroup

Write-Host "Computer is being added to groups, please wait" -foregroundcolor "red"

Function Start-Countdown{

ForEach ($Count in (1..$Seconds))
{ Write-Progress -Id 1 -Activity $Message -Status "Waiting for $Seconds seconds, $($Seconds - $Count) left" -PercentComplete (($Count / $Seconds) * 100)
Start-Sleep -Seconds 1
}
Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed
}

Start-Countdown -Seconds 10 -Message "Adding to Group..."

Write-Host "Computer $smartname now has the following AD Group Memberships" -foregroundcolor "green"

(Get-ADPrincipalGroupMembership (Get-ADComputer "$smartname").distinguishedname | select name).name

Write-Host "Press any key to close ..." -foregroundcolor "green"

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")



List Computers AD Memberships
call with .\scriptname.ps1 -smartname {computer}




param (
[string]$smartname
)

Import-Module ActiveDirectory



Write-Host "Computer has the following AD Group Memberships" -foregroundcolor "green"

(Get-ADPrincipalGroupMembership (Get-ADComputer "$smartname").distinguishedname | select name).name

Write-Host "Press any key to close ..." -foregroundcolor "green"

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")




SCCM Remote Control
call with .\scriptname.ps1 -smartname {computer}




param (
[string]$smartname
)

C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe 1 $smartname





4 REPLIES 4
servicedesk
Champion Sweeper II
To avoid that "issue", I call PS scripts like:

powershell -noprofile -ExecutionPolicy bypass -command {actionpath}script.ps1 {assetname}
Kboyer
Engaged Sweeper III
Yup, Did that ...working Thx

dpadgett
Engaged Sweeper II
set-executionpolicy bypass in command line
Kboyer
Engaged Sweeper III
Can't seem to get it to work fully. When enacting the action the Powershell window flashed briefly then goes away.
The script runs when I use a defined "smartname" within the PS window by itself
we call it using
{actionpath}\example.ps1 -smartname {computer}

*Update: Determined that trying to run script had PowerShell execution policy (run) disabled on local client PC's