Quantcast
Channel: Windows PowerShell forum
Viewing all 21975 articles
Browse latest View live

Powershell Question

$
0
0

Gentlemen,

I created the below PS command to get a report of remaining license count in Azure tenant. I need assistant with create another field to get the remaining license count in percentage and if it's below certain percentage change color to red or something.

 "Get-msolAccountSku | Select @{N='Product Name';E={switch ($_.AccountSkuId) { '<company domain>:ENTERPRISEPREMIUM' {'Office 365 Enterprise E5'} '<company domain>:POWER_BI_PRO' {'Power BI Pro'} '<company domain>:ENTERPRISEPACK' {'Office 365 Enterprise E3'} '<company domain>:SHAREPOINTSTORAGE' {'SharePoint Online Storage'} '<company domain>:POWER_BI_STANDARD' {'Power BI (free)'} '<company domain>:EMS' {'Enterprise Mobility + Security E3'} '<company domain>:MCOMEETADV' {'Audio Conferencing'} '<company domain>:DYN365_ENTERPRISE_SALES' {'Dynamics 365 for Sales Enterprise Edition'} '<company domain>:DYN365_ENTERPRISE_TEAM_MEMBERS' {'Dynamics 365 for Team Members Enterprise Edition'} default {'Unknown'} }}}, @{N='Active Units';E={$_.ActiveUnits}}, @{N='Consumed Units';E={$_.ConsumedUnits}}, @{Name = 'RemainingUnits'; Expression = {[string]([int]( $_.ActiveUnits - $_.ConsumedUnits))}}, @{N='Account SkuId'; E={$_.AccountSkuId}} | ConvertTo-Html"

thank you

Lrok


Lrok


use powershell to disable - enable rule - unknown behaviour

$
0
0

I am using windows 10 1909 latest version, with admin privilege's. 

Ensure that you have rule for inbound and outbound.

Now you block rule from both direction, now enable the rule only for outbound, to notice all the rule are vanished from inbound and appearing in the outbound, some unknown behavior, any solution please advise how do I get the my inbound rules back.

Below is the example:

PS C:\Windows\system32> get-netfirewallrule -displayname "*Media center*" | select displayname,Direction,Action                                                                                                                                 DisplayName                                            Direction Action                                                 -----------                                            --------- ------                                                 Media Center Extenders - UPnP (TCP-Out)                 Outbound  Allow                                                 Media Center Extenders - Media Streaming (UDP-Out)      Outbound  Allow
Media Center Extenders - Device Validation (TCP-Out)    Outbound  Allow
Media Center Extenders - Media Streaming (TCP-Out)      Outbound  Allow
Media Center Extenders - Media Streaming (TCP-In)        Inbound  Allow
Media Center Extenders - Device Provisioning (TCP-Out)  Outbound  Allow
Media Center Extenders - Service (TCP-Out)              Outbound  Allow
Media Center Extenders - WMDRM-ND/RTP/RTCP (UDP-Out)    Outbound  Allow
Media Center Extenders - WMDRM-ND/RTP/RTCP (UDP-In)      Inbound  Allow
Media Center Extenders - XSP (TCP-In)                    Inbound  Allow
Media Center Extenders - HTTP Streaming (TCP-In)         Inbound  Allow
Media Center Extenders - qWave (TCP-Out)                Outbound  Allow
Media Center Extenders - qWave (TCP-In)                  Inbound  Allow
Media Center Extenders - qWave (UDP-Out)                Outbound  Allow
Media Center Extenders - qWave (UDP-In)                  Inbound  Allow
Media Center Extenders - RTSP (TCP-Out)                 Outbound  Allow
Media Center Extenders - RTSP (TCP-In)                   Inbound  Allow
Media Center Extenders - SSDP (UDP-Out)                 Outbound  Allow
Media Center Extenders - SSDP (UDP-In)                   Inbound  Allow


PS C:\Windows\system32> set-netfirewallrule -displayname "*media center*" -Action Block                                 PS C:\Windows\system32> get-netfirewallrule -displayname "*Media center*" | select displayname,Direction,Action                                                                                                                                 DisplayName                                            Direction Action                                                 -----------                                            --------- ------                                                 Media Center Extenders - UPnP (TCP-Out)                 Outbound  Block                                                 Media Center Extenders - Media Streaming (UDP-Out)      Outbound  Block
Media Center Extenders - Device Validation (TCP-Out)    Outbound  Block
Media Center Extenders - Media Streaming (TCP-Out)      Outbound  Block
Media Center Extenders - Media Streaming (TCP-In)        Inbound  Block
Media Center Extenders - Device Provisioning (TCP-Out)  Outbound  Block
Media Center Extenders - Service (TCP-Out)              Outbound  Block
Media Center Extenders - WMDRM-ND/RTP/RTCP (UDP-Out)    Outbound  Block
Media Center Extenders - WMDRM-ND/RTP/RTCP (UDP-In)      Inbound  Block
Media Center Extenders - XSP (TCP-In)                    Inbound  Block
Media Center Extenders - HTTP Streaming (TCP-In)         Inbound  Block
Media Center Extenders - qWave (TCP-Out)                Outbound  Block
Media Center Extenders - qWave (TCP-In)                  Inbound  Block
Media Center Extenders - qWave (UDP-Out)                Outbound  Block
Media Center Extenders - qWave (UDP-In)                  Inbound  Block
Media Center Extenders - RTSP (TCP-Out)                 Outbound  Block
Media Center Extenders - RTSP (TCP-In)                   Inbound  Block
Media Center Extenders - SSDP (UDP-Out)                 Outbound  Block
Media Center Extenders - SSDP (UDP-In)                   Inbound  Block


PS C:\Windows\system32> set-netfirewallrule -displayname "*media center*" -Action Allow -Direction Outbound             PS C:\Windows\system32> get-netfirewallrule -displayname "*Media center*" | select displayname,Direction,Action                                                                                                                                 DisplayName                                            Direction Action                                                 -----------                                            --------- ------                                                 Media Center Extenders - UPnP (TCP-Out)                 Outbound  Allow                                                 Media Center Extenders - Media Streaming (UDP-Out)      Outbound  Allow
Media Center Extenders - Device Validation (TCP-Out)    Outbound  Allow
Media Center Extenders - Media Streaming (TCP-Out)      Outbound  Allow
Media Center Extenders - Media Streaming (TCP-In)       Outbound  Allow
Media Center Extenders - Device Provisioning (TCP-Out)  Outbound  Allow
Media Center Extenders - Service (TCP-Out)              Outbound  Allow
Media Center Extenders - WMDRM-ND/RTP/RTCP (UDP-Out)    Outbound  Allow
Media Center Extenders - WMDRM-ND/RTP/RTCP (UDP-In)     Outbound  Allow
Media Center Extenders - XSP (TCP-In)                   Outbound  Allow
Media Center Extenders - HTTP Streaming (TCP-In)        Outbound  Allow
Media Center Extenders - qWave (TCP-Out)                Outbound  Allow
Media Center Extenders - qWave (TCP-In)                 Outbound  Allow
Media Center Extenders - qWave (UDP-Out)                Outbound  Allow
Media Center Extenders - qWave (UDP-In)                 Outbound  Allow
Media Center Extenders - RTSP (TCP-Out)                 Outbound  Allow
Media Center Extenders - RTSP (TCP-In)                  Outbound  Allow
Media Center Extenders - SSDP (UDP-Out)                 Outbound  Allow
Media Center Extenders - SSDP (UDP-In)                  Outbound  Allow



How to filter the results?

$
0
0

Hi,

is it possible to filter only the results with the word (True) from that script?

I wanted to put in the form of (;) to filter in excel.

Get-ADUser -Filter * -SearchBase "OU=SomeOU,DC=mydomain,DC=forest,DC=local" | ForEach {
   $_.SamAccountName
   (new-object directoryservices.directoryentry "", ("domain\" + $_.SamAccountName), "").psbase.name -ne $null
   Write-Host ""
}



Result:

Guest
False

krbtgt
True

I want it like this:

Guest; False
krbtgt, True

Which only shows True in table format.

Thanks.




Powershell - Join duplicates into single row - SOLVED

$
0
0

This code gets's VM name, IP and MAC address

 $results = Get-VM | Where State –eq Running | Get-
VMNetworkAdapter | ForEach-Object {

    [pscustomobject]@{

        'VM NAME'      = $_.VMName

        'IP ADDRESS'   = ($_.IPAddresses -notmatch ':') -join " "

        'MAC ADDRESS'  = ($_.MacAddress -replace '(..)(..)(..)(..)(..)','$1:$2:$3:$4:$5:') -join ' '

        'HYPER-V HOST' = $env:COMPUTERNAME
        

    }
}

Some machines have multiple IP/MAC and new row for each IP/MAC is created

VM NAME   IP ADDRESS     MAC ADDRESS            HYPER-V HOST

OAP80     192.168.1.45       00:15:5D:58:12:5E     SERVER1                                            
OAP80     192.168.2.45       00:15:5D:58:12:5F     SERVER1                                             

 

How to modify above code to put all IP's/MAC addresses to single line

VM NAME             IP ADDRESS                      MAC ADDRESS                                      HYPER-V HOST

OAP80        192.168.1.45 192.168.2.45      00:15:5D:58:12:5E 00:15:5D:58:12:5F     server1

 


scriptblock and output variable

$
0
0

Hi All.

I have a Ps script which a lot of write-output commands, this script is within a script block that uses WinRM. I want to return a value so that the value can be used elsewhere, the problem I have is that I used

Return $filename

    $file_details = Invoke-Command -ComputerName $server -Credential $Credentials -ScriptBlock $ScriptBlock -ErrorAction Stop 

The Issue is that the script has a lot of output mesages, and then at the end is the filename.

All I want to do is extract the filename.

Is there a way to do this ?

Thanks in advance.

Powershell Script

$
0
0
How to write a powershell script connecting to a list of computers in the domain, searching for ost file in the C:\Users folder and then writing that output to the CSV file with human readable size format.

How to find Process's running inside a VM and Change Priority

$
0
0

I have a VM running in Windows. I can do a Get-VM() list it. I can do Get-Process() and get its name as vmwp.exe and ID. Combine the info got from both the commands and change the priority of the VM using SetPriorityClass().

My question is can i peek inside the VM and list all the processes running inside it, get their name and PID and then change the priority of the process of my desire? If you can kindly provide the PowerShell command for it, that would be awesome. 

I Googled and tried for half a day but could not tie all info. So I have to not get the PID of the process running inside the VM but be able set its priority too. 

thanks

ananda

Process cannot access the file

$
0
0

I am using below code to upload the files on a premise local folder to sharepoint Office 365.

#Upload file
Foreach ($File in (dir $Folder -File))
{
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $Upload = $FolderToUpload.Files.Add($FileCreationInfo)
    $Context.Load($Upload)
    $Context.ExecuteQuery()
}

I got an error: 

BFile> C:\DBFile\Upload to sharepoint.ps1
New-Object : Exception calling ".ctor" with "2" argument(s): "The process cannot access the file 'C:\dbfiles\myfile.csv' because it is being used by another process."
At C:\DBFile\Upload to sharepoint.ps1:51 char:19
+ ... ileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMo ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
 

The file or file is not open, but not sure why it says it is used by another process. searched online it says that file could be locked somehow. it is related with the filestream open. Is there a way to fix this? Thanks


Thanks


How set alias or a function to "cd C:\some\path" ?

$
0
0

I want to set an alias to a command "cd \some\very\long\dir\path\", so that instead of writing the entire path, I could simply type two letters.

I tried:

Set-Alias -Name fp -Value cd C:\Users\some\dir\

And got error "A positional parameter cannot be found that accepts argument"

Then I tried:

function fp { set-location "cd C:\Users\some\dir\" }

and got error "fp : The term ... is not recognized as the name of a cmdlet..."

How could I set an alias so that when I type "fp" and hit enter, I am being navigated to C:\Users\some\dir\ ?

EDIT: I also created "profile.ps1" (containing the function) in "C:\Users\User" and restarted PS instance, but still no luck

Thanks


make changes for unchecked boxes

$
0
0

this code makes changes to checked items, i’d like it to make changes for the unchecked items as well in the same line but i was unsuccessful of getting it to work. can someone help me pls ?

$objTypeCheckbox = New-Object System.Windows.Forms.CheckedListbox
$objTypeCheckbox.Location = New-Object System.Drawing.Point(10,40)
$objTypeCheckbox.Size = New-Object System.Drawing.Size(260,20)
$objTypeCheckbox.Height = 80
import-csv 'C:\scripts\gpos.csv' | ForEach-Object {
$objTypeCheckbox.Items.Add($_.Displayname)
$objTypeCheckbox.CheckOnClick=$true
}
$form.Controls.Add($objTypeCheckbox)
$form.Topmost = $true
$result = $form.ShowDialog()
$us = [Microsoft.VisualBasic.Interaction]::InputBox("Select user", "user name")
$permissionLevel = if ($result -eq [System.Windows.Forms.DialogResult]::Yes) { 'GpoApply' } else {'' }
foreach ($checkedPolicy in $objTypeCheckbox.CheckedItems) {
    Write-Host "Applying $permissionLevel to $checkedPolicy"
     Set-GPPermissions -Name $checkedPolicy -PermissionLevel $permissionLevel -TargetName $us -TargetType User -Domain domain.domain
}

thank you.


Getting output value from invoke-command

$
0
0

Hi All,

I have the code snippet below.

$scriptblock =
{
    $filename = 'D:\profile\test\summary.txt'
    write-output "Getting the file information"
    write-output "Import complete"
    return $filename
}

$result = Invoke-Command -ScriptBlock $scriptblock

The above is similar to a problem that I am trying to solve, i want to get the value of the filename outside the script block.

I was wondering if some string manipulation is the answer.

Adding notes to AD field with line breaks from a CSV

$
0
0

Hello,

I have a script that creates new groups from a CSV.  The issue I am having is with the notes field. I have a column headed notes.  The column contains multiple lines with spaces. However when I create my group the notes are all clobbered together.  How can I update the notes field while preserving the lines breaks in my CSV?  An example of the notes field with the header is below.

Notes

Hi technet

I can't get my notes field to preserve

Line breaks when creating new AD Groups

Can you help.

SQL compression backup not working

$
0
0

Hi,

I am running the below script to backup my SQL database server via Powershell to a local folder the script works fine it just doesn't compress the sql databases, can anyone help me figure out why?

#region
    # Backup SQL Server Databases - exclude System Databases like master, tempdb etc.
    $serverName = $env:ComputerName
    $backupDirectory = "C:\temp\Backup"
    $TestBackupDirectory = Test-Path $backupDirectory
    If ($TestBackupDirectory -eq $false)
    {New-Item -ItemType Directory -Force -Path "C:\Temp\Backup"}
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
    $mySrvConn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
    $mySrvConn.ServerInstance=$serverName 

    $server = new-object Microsoft.SqlServer.Management.SMO.Server($mySrvConn)
    

    $dbs = $server.Databases
        
    foreach ($database in $dbs | where-object { $_.IsSystemObject -eq $False})
    {
        $dbName = $database.Name      
        $timestamp = Get-Date -format yyyy_MM_dd
         $targetPath = $backupDirectory + "\" + $dbName + "_" + $timestamp + ".bak"

        $smoBackup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")
        $smoBackup.Action = "Database"
        $smoBackup.BackupSetDescription = "Full Backup of " + $dbName
        $smoBackup.BackupSetName = $dbName + " Backup"
        $smoBackup.Database = $dbName
        $smoBackup.MediaDescription = "Disk"
        $smoBackup.Devices.AddDevice($targetPath, "File")
        $smoBackup.SqlBackup($server)
        $smoBackup.CompressionOption = "1""Backed up $dbName ($serverName) to $targetPath"
    }
#endregion


MCITP:Server Administration|MCTS: Server 2008| MCSE: Server 2003| MCSA + M: Exchange Server 2003| MCP: Windows XP|

Powershell script to delete profiles remotely

$
0
0

Looking for a script to delete windows profiles remotely, I have the below script but this only works locally tried a few on different sites but cant get them working very new to power shell

$Computer = Read-Host “Please Enter Computer Name: ”
$user = Read-Host “Enter User ID: ”

Invoke-Command -ComputerName $computer -ScriptBlock {
param($user)
$localpath = ‘c:\users\’ + $user
Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq $localpath} |
Remove-WmiObject
} -ArgumentList $use

Variable not loading

$
0
0

Hi,

I'm trying to get the textbox to display the first line of the File, 

 

However when I click each button the text file has “System.Management.Automation.PSVariable”



Any Help?


$form=New-Object System.Windows.Forms.Form

function function-to-run{

$search = $This.text

$Email = Get-Variable -Name Email$search

$textUpdate.Text = $Email

}

 

$i=0

$files = (Get-ChildItem H:\People\*.txt).BaseName

foreach ($file in $files) {

 

    $b=New-Object System.Windows.Forms.Button

    $b.Name=$file

     $b.location="0,$(25*++$I)"

      $b.size='100,20'

     $b.Text="$file"

     $b.Tag=$file

     $b.add_click({  function-to-run })

Set-Variable -name Email$file -Value $file

    $form.controls.add($b)

}

$textUpdate = New-Object Windows.Forms.TextBox

$textUpdate.Location = New-Object Drawing.Point 0,0

$textUpdate.Size = New-Object Drawing.Point 250,30

$textUpdate.Text = ""

 

$form.controls.add($textUpdate)

$form.showdialog()

Thank you!


Powershell - Missing closing '}' in statement block or type definition.

$
0
0

Hello,

Wondering if anyone can assist why my below PS script block returns back with:

At C:\scripts\Test-RemoveO365Owner.ps1:5 char:34
+     foreach ($group in $O365Groups) {Remove-UnifiedGroupLinks –Iden ...
+                                     ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

$O365Groups = (Get-AzureADUserOwnedObject -ObjectId $upnIn -All $true).displayname
	foreach ($group in $O365Groups) {
        Remove-UnifiedGroupLinks –Identity $group –LinkType Owners –Links $upnIn
    }
Write-Host "O365 Group Ownership Removed"

Thanks!


Getting a Dropdown to work

$
0
0

Hello,

I'm trying to get a dropdown to list out several templates for our Powershell script. I have used Radio Buttons to list the templates for now here is an excerp from what I have am doing so far...

Thanks for the help!

$OS_rb1 = New-Object System.Windows.Forms.RadioButton
$OS_rb2 = New-Object System.Windows.Forms.RadioButton
$OS_rb3 = New-Object System.Windows.Forms.RadioButton
$OS_rb4 = New-Object System.Windows.Forms.RadioButton

$GroupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 407
$System_Drawing_Point.Y = 3
$GroupBox1.Location = $System_Drawing_Point
$GroupBox1.Name = "GroupBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 298
$System_Drawing_Size.Width = 302
$GroupBox1.Size = $System_Drawing_Size
$GroupBox1.TabIndex = 30
$GroupBox1.TabStop = $False

$form1.Controls.Add($GroupBox1)

$OS_rb1.DataBindings.DefaultDataSourceUpdateMode = 0
$OS_rb1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 114
$System_Drawing_Point.Y = 39
$OS_rb1.Location = $System_Drawing_Point
$OS_rb1.Name = "OS_rb1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 66
$OS_rb1.Size = $System_Drawing_Size
$OS_rb1.TabIndex = 5
$OS_rb1.Text = "2012r2"
$OS_rb1.UseVisualStyleBackColor = $True

$GroupBox1.Controls.Add($OS_rb1)


$OS_rb2.DataBindings.DefaultDataSourceUpdateMode = 0
$OS_rb2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 114
$System_Drawing_Point.Y = 64
$OS_rb2.Location = $System_Drawing_Point
$OS_rb2.Name = "OS_rb2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 66
$OS_rb2.Size = $System_Drawing_Size
$OS_rb2.TabIndex = 6
$OS_rb2.Text = "RHEL7"
$OS_rb2.UseVisualStyleBackColor = $True

$GroupBox1.Controls.Add($OS_rb2)


$OS_rb3.DataBindings.DefaultDataSourceUpdateMode = 0
$OS_rb3.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 194
$System_Drawing_Point.Y = 39
$OS_rb3.Location = $System_Drawing_Point
$OS_rb3.Name = "OS_rb3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 98
$OS_rb3.Size = $System_Drawing_Size
$OS_rb3.TabIndex = 0
$OS_rb3.TabStop = $True
$OS_rb3.Text = "2016_GUI"
$OS_rb3.UseVisualStyleBackColor = $True

$GroupBox1.Controls.Add($OS_rb3)


$OS_rb4.DataBindings.DefaultDataSourceUpdateMode = 0
$OS_rb4.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 194
$System_Drawing_Point.Y = 64
$OS_rb4.Location = $System_Drawing_Point
$OS_rb4.Name = "OS_rb4"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 97
$OS_rb4.Size = $System_Drawing_Size
$OS_rb4.TabIndex = 1
$OS_rb4.TabStop = $True
$OS_rb4.Text = "2016_Core"
$OS_rb4.UseVisualStyleBackColor = $True

$GroupBox1.Controls.Add($OS_rb4)

Extracting values from json

$
0
0

Hi All.

I have output values similar to the below from a scriptblock.

I would like to extract the value of step 2.

Is there a specific way to do this as i am not sure about the formatting.

The data type is json, I tried to convert this to a hash table but not sure if thats the best way, from what I see one should be able to convert from json back.

{"Step1":  "Running step 1"
}
{"Step2":  "Running step 2."
}
{"Step3":  "Running step 3"
}
{"Step4":  "Running step 4"
}

Thanks in advance.

$p = Invoke-WebRequest -Uri $url

$
0
0

When using below to load a webpage, it will take 4+ seconds. Is there a way to load it faster? Thanks.

$p = Invoke-WebRequest -Uri $url

Rejected credentials

$
0
0

Hi!

I'm trying to execute a PS1 script after deployment of a server. This PS1 script is fired using SetupComplete.cmd.

$domain= "ourdomain.local"
$password= "password" | ConvertTo-SecureString -asPlainText -Force
$user= "$domain\domainadminuser"
$cred= New-Object System.Management.Automation.PSCredential($user,$password)
$server= "dc.ourdomain.local"
$oupath= "OU=Windows 2019 Servers,OU=Member Servers,OU=Resources,OU=Data,DC=ourdomain,DC=local"
Add-Computer -DomainName $domain -OUPath $oupath -Credential $cred

#pull info to add computer to correct local groups
$group= Get-ADGroup "CN=Standard Servers,OU=Server,OU=Groups,OU=Resources,OU=Data,DC=ourdomain,DC=local" –Server $server
$hostname= hostname
$computerName= Get-ADComputer $hostname –Server $server

#add computer to local group
Add-ADGroupMember $group -Member $computerName -Server $server -Credential $cred
Restart-Computer
The script joins the domain in the right OU - check! But trying to add the server to a specified security group, it fails. I get a "Get-ADGroup : The server has rejected the client credentials

Viewing all 21975 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>