Powershell Script for adding users to AD - powershell

Hi I've just resently started to use powershell on my server. Though when I run the script I get the error:
New-ADUser : Unable to find a default server with Active Directory Web Services running.
At C:\Users\Administrator\Desktop\Powerwhell Script, H1 case.ps1:6 char:1
+ New-ADUser -name $_."fornavn"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-ADUser], ADServerDownException
+ FullyQualifiedErrorId : ActiveDirectoryServer:1355,Microsoft.ActiveDirectory.Management.Commands.NewADUser
I have attached the script and my .csv file. Hope any of you can help me figuring it out.
(Don't worry about the information, it's for a school assignment)
Script
.csv file

It looks like your script can not find the domain control on your domain. Simply use the -Server parameter and give it the Full Qualified Domain Name or IP of the domain control.
New-ADUser -Server "ServerName.Domain.com"
If this doesn't work you might not have Active Directory Management Gateway Service installed on your domain control (Download Here). With Windows server 2012 R2 make sure you have the following feature installed.
The headers warning you are seeing is because Import-Csv is unable to get the headings from your CSV file for some reason, and replaces the header name with H1,H2 ... Hx. For example:
fornavn efternavn H1 beskrivelse, ...
------- --------- ----- -----------
Keld Bruun KB Adm.Ledergruppe, ...
You can get round this you can giving Import-Csv the names of your columns via the -Header parameter. Note that these do not have to be the same as the ones in the CSV, as the new column headers will overwrite the CSV.
Import-Csv "C:\H1, Powershell.csv" -Header 'fornavn','efternavn','forkortelse','beskrivelse','email','brugernavn','kode','kontor','fuldnavn'

Related

Edit/Add GPO on DC via Powershell

I'm given the task to migrate all the printers installed on workstations via GPO to another server.
As for now all printers are installed in a local decentralized Distribution Point, we want to move on a centralized Distribution Point/Print Server.
On mine DC, via Group Policy Management Editor, I've a lot of printers in
Computer Configuration\Preferences\Control Panel Settings\Printers
All printers are mapped from \DP00x\Printer and given a local name.
What i want to change is the \DP00x to \CentralDP01\Printer in the GPO.
I've managed via powershell to create all printer ports, install all printers and publish/list in the directory all of them.
Given that they are more than 100, I wish to automate the process to edit the GPO editing, so that i don't need to open each policy and each printer to modify the destination.
I've tried the cmdlet Get-GPRegistryValue because I know (at least) that printers are installed on HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers
but i get this error every time:
Get-GPRegistryValue : The following Group Policy registry setting was not found: "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers".
Parameter name: keyPath
At line:1 char:1
+ Get-GPRegistryValue -Guid 6b464ed9-66c8-47fa-8327-1fe9b074a0d7 -Key H...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Group...tryValueCommand:GetGPRegistryValueCommand) [Get-GPRegistryValue], ArgumentException
+ FullyQualifiedErrorId : UnableToRetrievePolicyRegistryItem,Microsoft.GroupPolicy.Commands.GetGPRegistryValueCommand
I tried as well Get-GPPrefRegistryValue
Get-GPPrefRegistryValue -Context Computer -Guid 6b464ed9-66c8-47fa-8327-1fe9b074a0d7 -Key HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers
But error looks the same:
Get-GPPrefRegistryValue : The Preference registry setting "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers" was not found in the
"x-x-x-x-x-x" GPO in the x-x-x-x-x-x-x.com domain.
Parameter name: keyPath
At line:1 char:1
+ Get-GPPrefRegistryValue -Context Computer -Guid 6b464ed9-66c8-47fa-83 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Group...tryValueCommand:GetGPPrefRegistryValueCommand) [Get-GPPrefRegistryValue], ArgumentException
+ FullyQualifiedErrorId : UnableToRetrievePreferenceRegistryItem,Microsoft.GroupPolicy.Commands.GetGPPrefRegistryValueCommand
I found a workaround. Backup the GPO, manually edit the XML with the new value and import back the GPO.
I don't fancy the idea of manually editing because it can lead to errors and with over 100+ GPOs I can have alot of errors.
Can anyone help me?
Maybe i'm using the wrong commands, but so far documentations state to use GPO Module.
Unfortunately the GroupPolicy commands are limited to registry key settings only, and printer-preferences fall outside that. You can safely edit the live GPO xml files themselves though (or use Backup-GPO/Restore-GPO).
If you're only replacing the server name, this should work fine. Try it on a test GPO, updating the path as needed:
$guid = (Get-GPO -Name 'Test GPO')
# Check the GPO version before changes:
Get-GPO -guid $guid
$domain = 'domain.com'
$path = "\\$domain\SYSVOL\$domain\Policies\{$guid}\User\Preferences\Printers\Printers.xml"
# Update the path in the GPO xml:
(Get-Content $path -Raw) -replace 'DP00x','CentralDP01' | Set-Content $path
# Validate the GPO version/change date have updated - might take a while if xml is on a different DC:
Get-GPO -guid $guid

powershell ver 2.0 search AD failure [duplicate]

We have mixed desktop operating systems consisting of windows 7 and windows 10. I have a login script that gathers various information from a powershell script that runs each time a user logs in. The windows 7 powershell is only version 2 which means I cannot use get-aduser, I am therefore challenged to query this information out of AD using a different method that would be compatible with both win7 and win10. I have this line of code that does not use get-aduser and successfully produces a list of all AD users on powershell 5(win10), however when I attempt to use it on powershell 2 it produces the error below.
My questions are these:
What do I need to change to get the script working on powershell version2 ?
How can I get it to output the current user as opposed to all the users in AD
thank you for any help in advance
<position> : The following exception was thrown when trying to enumerate the collection: "Configuration system failed t
o initialize".
At line:1 char:1
+ <<<< (New-Object DirectoryServices.DirectorySearcher "ObjectClass=user").FindAll() | Select-object -property path
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : ExceptionInGetEnumerator
(New-Object DirectoryServices.DirectorySearcher “ObjectClass=user”).FindAll() | Select-object -property path

query AD using powershell version 2

We have mixed desktop operating systems consisting of windows 7 and windows 10. I have a login script that gathers various information from a powershell script that runs each time a user logs in. The windows 7 powershell is only version 2 which means I cannot use get-aduser, I am therefore challenged to query this information out of AD using a different method that would be compatible with both win7 and win10. I have this line of code that does not use get-aduser and successfully produces a list of all AD users on powershell 5(win10), however when I attempt to use it on powershell 2 it produces the error below.
My questions are these:
What do I need to change to get the script working on powershell version2 ?
How can I get it to output the current user as opposed to all the users in AD
thank you for any help in advance
<position> : The following exception was thrown when trying to enumerate the collection: "Configuration system failed t
o initialize".
At line:1 char:1
+ <<<< (New-Object DirectoryServices.DirectorySearcher "ObjectClass=user").FindAll() | Select-object -property path
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : ExceptionInGetEnumerator
(New-Object DirectoryServices.DirectorySearcher “ObjectClass=user”).FindAll() | Select-object -property path

Out-file export to a network location powershell

I am having an issue trying to output a file with PowerShell, I can export the file to the computer's local drives however when I want to export it to a network location it will not let me.
I receive the following error:
Access to the path '\\fmadt-prod-web5\e$\ftproot\customer\temp\SiteLists\Classic\Hosted1.txt' is denied.
+ CategoryInfo : OpenError: (:) [Out-File], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
This is the code that I am using:
$list2 | Format-Table -a -Property "WebAppName", "Version", "State"| Out-File '\\fmadt-prod-web5\e$\ftproot\customer\temp\SiteLists\Classic\Hosted1.txt' -force
Is it possible to export to a network location? The user I am using has admin access to that location as well.
You can't use a UNC path, but you can map a PSDrive to that location and use that:
New-PSDrive -Name dest -Root \\fmadt-prod-web5\e$\ftproot\customer\temp\SiteLists\Classic -PSProvider FileSystem
Then:
| out-file dest:\Hosted1.txt
Use the -Credential parameter of New-PSDrive if you need to access the drive using alternate credentials.
you most certainly can output to unc paths, I regularly use those at work. This almost looks like you don't, or the account you are running the script as doesn't, have access to the directory.
E$ refers to the admin share on a server, try actually sharing that directory via windows shares or run the script using an account that is in the administrators group on the relevant server. Also, I always use double quotes for paths because then you can include variables -- force of habit :)

Start FTP Website from powershell 4

Trying to automate ftp site manipulation on IIS7.5, through powershell, but I can't start the ftp site. Everything else, so far, I succeeded.
PS IIS:\Sites> Get-ChildItem | Where {$_.Name -eq "FtpData"}
Name ID State Physical Path Bindings
---- -- ----- ------------- --------
FtpData 3 Stopped D:\Services\_Empty ftp *:80:
PS IIS:\Sites> Start-WebSite -Name "FtpData"
Start-WebSite : The object identifier does not represent a valid object. (Exception from HRESULT: 0x800710D8)
At line:1 char:1
+ Start-WebSite -Name "FtpData"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Website], COMException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.IIs.PowerShell.Provider.StartWebsiteCommand
This issue is address in technet "Starting / Stoping an FTP site using PowerShell... IIS 7.5 on 2008R2" but is has three years old.
Using the appcmd has the same issue:
C:\Users\myself>c:\Windows\system32\inetsrv\appcmd start site FtpData
ERROR ( hresult:800710d8, message:Command execution failed.
The object identifier does not represent a valid object.
)
This article FTP on IIS 7 on Server Core indicates to start it from the UI Console, but that means it can't be automated.
Has any one got a solution to this?
Stumbled upon this through Powershell autocomplete and turned out working on Server 2012 R2:
$ftpSite = IIS:\Sites\FtpData
$ftpSite.ftpServer.Start()
The $ftpSite... did not work for me. I also found the below to be neater.
To Start:
(get-Website -Name "myftpsite").ftpserver.start()
To Stop:
(get-Website -Name "myftpsite").ftpserver.stop()
Source: https://peter.hahndorf.eu/blog/iisftp.html
(Forever Breathes The Lonely Word - Peter Hahndorf on software
IIS - Managing FTP sites with PowerShell)