Test user exists with Powershell inputbox - windows

I am trying to script a little application that would make possible to copy AD user's security groups and paste them to another one's profile.
I am good with this part but I want to make it a little bit more difficult by implementing some input boxes that would search for the AD user, errors out in case it does not exit in my AD and prompt once again until user has been found.
ipmo activedirectory
Add-type -assemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
$userref = [Microsoft.VisualBasic.Interaction]::Inputbox("Enter username
", "Prime User")
$usertar = [Microsoft.VisualBasic.Interaction]::Inputbox("Enter username",
"Target")
$userref, $usertar | foreach {
if ([bool](Get-ADUser -Filter {samaccountname -eq $_}) -ne $true) {
[System.Windows.Forms.MessageBox]::Show("This user does not exist!")
}
else {Write-Host "User Ok"}
}

Since you need to validate the existance of two AD users before the rest of your code can run, you are basically asking the same thing twice using inputboxes.
In that case I would suggest adding a small custom function to do that.
Something like this perhaps:
Import-Module ActiveDirectory
Add-type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
function Get-UserFromInputbox ([string]$Title) {
do {
$account = [Microsoft.VisualBasic.Interaction]::Inputbox("Enter user accountname", $Title)
# On Cancel the InputBox function simply returns an empty string.
# in that case, just return $null so the calling code can handle it
if ([string]::IsNullOrEmpty($account)) { return $null }
# Check if the user can be found
$user = Get-ADUser -Filter "SamAccountName -eq '$account'" –Properties MemberOf -ErrorAction SilentlyContinue
if (!$user) {
# If not found, show the same InputBox again until a valid
# accountname was given or the dialog is cancelled.
[System.Windows.Forms.MessageBox]::Show("User '$account' does not exist!")
}
}
while (!$user)
return $user
}
# Get the AD User object for the source user
$userref = Get-UserFromInputbox -Title "Source User"
if (!$userref) { exit }
# Ditto for the target user
$usertar = Get-UserFromInputbox -Title "Target User"
if (!$usertar) { exit }
# From here on you should have two valid AD user objects with the default properties `DistinguishedName, Enabled,
# GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName`.
# In the function we extended that to also have the `MemberOf` property.
Hope this helps

Related

Add PS Profile to a new local user

I want to create a Powershell profile for a new local user. This new local user is just a copy of Administrator account.
My function function works when profile has been initialized. But it don't work when the Windows profile is not created yet (user still not logged in since the creation of account).
Here is my try:
function add-PSProfile
{
[CmdletBinding()]
Param
(
[Parameter( Mandatory=$true )]
$Login,
[Parameter( Mandatory=$true )]
[ValidateScript({Test-Path $_})]
$RemoteProfilePath
)
# Get default profiles directory
$ProfilesDirectory = ( Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' -ErrorAction Stop ).ProfilesDirectory
$ProfilePath = Join-Path $ProfilesDirectory $Login
$PSProfilePath = Join-Path $ProfilePath 'Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'
if( -not ( Test-Path $PSProfilePath ) )
{
New-Item -ItemType File -Path $PSProfilePath -Force
}
Copy-Item $RemoteProfilePath $PSProfilePath
}
Do you have any suggestion?
Thanks for your help :)

How to filter Microsoft Service using PowerShell

How to filter the services that belong to Microsoft [including applications such as Office, IIS, AD, EXCH and so on] using PowerShell [v1.0 preferably].
In the "msconfig.msc -> Services Tab" there is a check box that reads "Hide all Microsoft Services", I'm looking for the same functionality of filtering the services that belong to MS.
Is this a possibility? Kindly assist.
This will give you a way to start:
Get-WmiObject Win32_Service -Property * | Select DisplayName,PathName | %{ Try { if([System.Diagnostics.FileVersionInfo]::GetVersionInfo("$($_.PathName.ToString().Split("-")[0].Split("/")[0])").LegalCopyright -like "*Microsoft*") {"$($_.DisplayName) is a Microsoft Service"}} catch {}}
I used the code above to make this. I was interested in all the non-MS services and a more versatile output format.
$services = Get-WmiObject Win32_Service -Property Name,DisplayName,PathName | Select
Name, DisplayName,PathName
$serviceList = New-Object System.Collections.ArrayList
foreach ($service in $services) {
Try {
$path = $service.Pathname.tostring().replace('"','')
$cri = ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($path)).legalcopyright
if ($cri -notlike "*Microsoft*") {
$serviceList += $service
}
} catch {}
}
$serviceList

How to get the IADSUser native object from directory entry through powershell?

I wanted to get the groups of a local user account in windows.This can be done if we get the native object from the directory entry. This is achieved in the following way through APIs :
DirectoryEntry comp = new DirectoryEntry("WinNT://computername");
DirectoryEntry de = comp.Children.Find("account19", "user");
IADsUser NativeObject = (IADsUser)directoryentry.NativeObject;
But how to get the same thing through powershell script?
You can use the Microsoft .NET Framework types in the System.DirectoryServices.AccountManagement namespace to obtain local group memberships. I wrote a simple PowerShell advanced function that will retrieve the group memberships for a local user account.
Note: Because we are using the GetGroups() method on the UserPrincipal class, this code is very efficient. You do not need to get a list of all groups, and then iterate over them, as previously suggested in the comments.
function Get-LocalUserGroupMembership {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
[string] $Identity = $env:USERNAME
)
# Import the System.DirectoryServices.AccountManagement .NET library
Add-Type -AssemblyName System.DirectoryServices.AccountManagement;
# Get a reference to the local machine's Security Account Manager (SAM)
$PrincipalContext = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList ([System.DirectoryServices.AccountManagement.ContextType]::Machine);
# Get a reference to a specific user principal, based on its account name
$UserAccount = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($PrincipalContext, $Identity);
if (!$UserAccount) {
throw 'User account could not be found!';
}
# Call the GetGroups() method on the UserPrincipal object
$GroupList = $UserAccount.GetGroups();
# Output the list of groups
Write-Output -InputObject $GroupList;
}
Get-LocalUserGroupMembership;

powershell windows form browes dialogue

Im trying to create a windows form that has a button when clicked will display a folder/file browes window, then the user selects the file/folder and clicks OK and i can then use the selected path as a string for another script.
the problem is that when i run it through PowerGUI (powershell scripting app) it works fine, but when i run through windows powershell it hangs when loading the browse dialog, anyone seen this before or see what ive done wrong or got an alternative, any help would be appreciated.
cls
$button = $browse = $form = 0
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$browse = new-object system.windows.Forms.FolderBrowserDialog
$browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
$browse.ShowNewFolderButton = $false
$browse.selectedPath = "C:\"
$browse.Description = "Choose a directory"
$button1 = New-Object system.Windows.Forms.Button
$button1.Text = "Choose Directory"
$button1.Add_Click({$browse.ShowDialog()})
$button1.left = 20
$button1.top = 20
$form = New-Object system.windows.forms.Form
$form.controls.add($button1)
$form.ShowDialog()
$form.Dispose()
$browse.SelectedPath
I was having a similar problem when running my script through PowerShellPlus (anther powershell editor). Luckily I found this post that shows how to prompt for a folder without using the FolderBrowserDialog. Here's the code that I'm using in a set of powershell functions I've written for prompting the user for many different kinds of input via a GUI.
# Show an Open Folder Dialog and return the directory selected by the user.
function Read-FolderBrowserDialog([string]$Message, [string]$InitialDirectory)
{
$app = New-Object -ComObject Shell.Application
$folder = $app.BrowseForFolder(0, $Message, 0, $InitialDirectory)
if ($folder) { return $folder.Self.Path } else { return '' }
}
Your code works when I try it. However I have noticed that sometimes(especially the 2nd time in a session) I use a browsewindow, it is hidden behind the PowerShell console and it seems like it's stuck. So can you try moving your powershell console to the side when it "hangs"?
Also, as a suggestion: if you're only using the form to select a folder location, I would skip it. You won't recieve the browser value until you close the form anyways, so try something like this instead:
function Get-BrowseLocation
{
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles()
$browse = New-Object System.Windows.Forms.FolderBrowserDialog
$browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
$browse.ShowNewFolderButton = $false
$browse.Description = "Choose a directory"
$loop = $true
while($loop)
{
if ($browse.ShowDialog() -eq "OK")
{
$loop = $false
} else
{
$res = [System.Windows.Forms.MessageBox]::Show("You clicked Cancel. Try again or exit script?", "Choose a directory", [System.Windows.Forms.MessageBoxButtons]::RetryCancel)
if($res -eq "Cancel")
{
#End script
return
}
}
}
$browse.SelectedPath
$browse.Dispose()
}
PS > Get-BrowseLocation
D:\
If you make the following changes to the function provided by Frode. F, the dialog will always come to the top.
$topform = New-Object System.Windows.Forms.Form
$topform.Topmost = $true
$topform.MinimizeBox = $true
$loop = $true
while($loop)
{
if ($browse.ShowDialog($topform) -eq "OK")
I think you're experiencing the issue I've faced, which is addressed in this question
The answer suggests setting .ShowHelp to $true, like this:
$openFileDialog = New-Object System.Windows.Forms.openFileDialog
$openFileDialog.ShowHelp = $true
$openFileDialog.ShowDialog() | Out-Null

Powershell remoting ApplicationArguments not working

I'm trying to pass some arguments into a new Powershell remoting session using the PSSessionOption's ApplicationArguments property. Unfortunately, the arguments don't seem to show up.
Here's the code:
$client = "Bubba"
$options = New-PSSessionOption -ApplicationArguments #{ Client = $client }
Enter-PSSession -ComputerName "Server" -SessionOption $options
$clientName = $PSSenderInfo.ApplicationArguments.Client
$dir = New-Item "c:\temp\$clientName" -type directory
Exit-PSSession
This results in an error: "NewNotImplementedException at offset 101 in file:line:column..."
Am I doing something wrong?
Apparently Enter-PSSession and Exit-PSSession don't work within a script! They're for interactive command-line use only. This explains the problem.

Resources