PowerShell Commands
33 TopicsPowerShell code does not work
PS C:\Foo> $PasswordHT = @{ >> String = 'Pa$$w0rd' >> AsPlainText = $true >> Force = $true >> } PS C:\Foo> $SecurePW = ConvertTo-SecureString @$PasswordHT ConvertTo-SecureString : La valeur de paramètre «@System.Collections.Hashtable» n’est pas une chaîne chiffrée valide. Au caractère Ligne:1 : 13 + $SecurePW = ConvertTo-SecureString @$PasswordHT + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument : (:) [ConvertTo-SecureString], PSArgumentException + FullyQualifiedErrorId : ImportSecureString_InvalidArgument,Microsoft.PowerShell.Commands.ConvertToSecureStringComma nd """"Why my code does not work, I tried to improve but I still have an error that returned to me.""""""Solved1.1KViews0likes4CommentsAdd workstations to Domain using powershell
Team, I am looking for a PS script to add multiple workstations to Domain, i used the below command to add, but firewall is blocking in adding and restarting, can some one help me with the correct script. script should take the workstations from txt file. Add-Computer -ComputerName $Computer -DomainName $domain -Credential $credential -Restart -Force .1.2KViews0likes2CommentsAdd a line to skip or continue loop execution
Hi I have a PS script which is terminating workflows in SharePoint. I don't want to run this script in one go rather I want to add user intervention as well. Below is my script portion. In line 44, I added a read host command to see which instance is being terminated. What I want is when I press yes then it executes the next command and terminate the workflow but if I press No then it skips the execution of next command and loop to next instance. How can I achieve that? Thanks Try{ #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) #Get the Web, List and List Item Objects $Web = $Ctx.Web $Ctx.Load($Web) $List = $Web.Lists.GetByTitle($ListName) $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) $Ctx.Load($List) $Ctx.Load($ListItems) $Ctx.ExecuteQuery() #Initialize Workflow Manager and other related objects $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web) $WorkflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService() $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService() $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id) $Ctx.Load($WorkflowAssociations) $Ctx.ExecuteQuery() #Loop through each List Item ForEach($ListItem in $ListItems) { #Get Workflow Instances of the List Item $WorkflowInstanceCollection = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItem.Id) $Ctx.Load($WorkflowInstanceCollection) $Ctx.ExecuteQuery() #Get all Workflow Instances in progress ForEach ($WorkflowInstance in $WorkflowInstanceCollection | Where {$_.Status -eq "Suspended"}) { [PSCustomObject] @{ ItemID = $ListItem.ID Status = $WorkflowInstance.Status WorkflowStarted = $WorkflowInstance.InstanceCreated WorkflowAssociation = $WorkflowAssociations | where {$_.ID -eq $WorkflowInstance.WorkflowSubscriptionId} | Select -ExpandProperty Name ItemUrl = "$($Web.Context.Web.Url)/$($workflowInstance.Properties["Microsoft.SharePoint.ActivationProperties.CurrentItemUrl"])" StartedBy = $workflowInstance.Properties["Microsoft.SharePoint.ActivationProperties.InitiatorUserId"] } $WorkflowInstanceService.TerminateWorkflow($WorkflowInstance) Read-Host -Prompt "Do you want to Terminate this instance:" $Ctx.ExecuteQuery() Write-host -f Green "'$($WorkflowAssociations | where {$_.ID -eq $WorkflowInstance.WorkflowSubscriptionId} | Select -ExpandProperty Name)'is Terminated" } } } Catch { Write-host -f Red "Error:" $_.Exception.Message }Solved4.1KViews0likes7CommentsPowershell: how to use an if condition with true false
Highlighted , how is $ True is equal to false? Same way, why does typecasting 'False' to boolean is evaluating to true? What is the right way to use if condition that can receive $True or 'True or $False or 'False' $Stat = $True if($Stat -eq 'fal'){Write-Host "if executed"}else{Write-Host "else block"} if($Stat -eq $False){Write-Host "if executed"}else{Write-Host "else block"} if($Stat -eq 'False'){Write-Host "if executed"}else{Write-Host "else block"} $Stat = 'True' if($Stat -eq 'False'){Write-Host "if executed"}else{Write-Host "else block"} if($Stat -eq 'True'){Write-Host "if executed"}else{Write-Host "else block"} if($Stat -eq $True){Write-Host "if executed"}else{Write-Host "else block"} if($Stat -eq $False){Write-Host "if executed"}else{Write-Host "else block"} $Stat = $False if($Stat){Write-Host "if executed"}else{Write-Host "else block"} $Stat = 'False' if($Stat){Write-Host "if executed"}else{Write-Host "else block"} if([bool]$Stat){Write-Host "if executed"}else{Write-Host "else block"} $dd = [bool]$Stat if($dd){Write-Host "if executed"}else{Write-Host "else block"}Solved121KViews0likes6CommentsComment Or Un-Comment Multiple Lines In PowerShell Script
Problem Statement:I have a PowerShell script and before executing it I want to comment multiple lines using PowerShell command Series Of Steps Need To Performed 1. Comment multiple lines in PowerShell script. 2. Execute the script. 3. Un-Comment commented or multiple lines in PowerShell script. Purpose: PowerShell scripts is having multiple steps to execute and I want to execute certain steps as per use cases by commenting & uncommenting the block of code. For Example:In-below PowerShell script I want to comment out second "if " block where string should match as "$RestartNumber -le 1" and get it's line number and put "<#" before if statement and put "#>" after the end of "if statement" curly braces "}". In notepad or any other editor , you can simply manually just put "<#" & "#>" before & after line to make whole if statement block commented & uncommented. Wondering How we can do it through powershell. If ($RestartNumber -eq 0) { $RestartNumber = Read-Host -Prompt 'Input number to restart the script at or <return> to start at the beginning: ' } If ($RestartNumber -le 1) { ExecuteOrSkipPause If ($Script:Continue) { foreach ($a in $b) { foreach ($c in $d) { Try { } Catch { } } } } } If ($RestartNumber -le 2) { ExecuteOrSkipPause If ($Script:Continue) { foreach ($e in $f) { foreach ($g in $h) { Try { } Catch { } } } } } If ($RestartNumber -le 3) { ExecuteOrSkipPause If ($Script:Continue) { foreach ($i in $j) { foreach ($k in $l) { Try { } Catch { } } } } } Thanks Vivek4.6KViews0likes7CommentsRegarding the different output formats of Powershell Versions 5 and 7.
I use Exchange Online commands through Powershell. I have noticed that Powershell versions 5 and 7 work differently. And I would like to achieve an output format like Powershell version 5, but without using the "|Format-table" notation. Is it possible to rewrite the default values? Thank you. Powershell version information in use: PowerShell 5:5.1.22000.653 and PowerShell 7: 7.2.5Solved1KViews0likes3CommentsPS Script: New-Team NotFound
Hi everyone, since 3 days I can not create a TEAMS via Powershell anymore. Its the same script since 3 months at it was never a problem. My script is: #TeamDisplayName-declareVariables $display_name="AytiTest3" $description_name="MCCH-ORG-EXTAytiTest3" $mailnick_name="MCCH-ORG-EXTAYTITEST3" $owner="my Company Email Adress" #PSforCreation New-Team-DisplayName$display_name-MailNickName$mailnick_name-Description$description_name-Owner$owner-AllowCreateUpdateRemoveConnectors:$false-AllowCreateUpdateRemoveTabs:$false-AllowAddRemoveApps:$false-AllowDeleteChannels:$false-AllowGuestDeleteChannel:$false-AllowCreateUpdateChannels:$false-Visibility"private" The Error message since today is: New-Team:NotFoundin/v1.0/teams/endpoint Atline:1char:2 +New-Team-DisplayName$display_name-MailNickName$mailnick_name-De... +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:NotSpecified:(:)[New-Team],HttpRequestException +FullyQualifiedErrorId:System.Net.Http.HttpRequestException,Microsoft.Teams.PowerShell.TeamsCmdlets.NewTeam Like I told you above, this script worked since 3 months without any problems. Any Idea what the Problem is? Regards Aytac1.9KViews0likes4CommentsHow to use output from 1 script in another script ??
I am trying to take the output from Script 1 and execute the actions in Script 2 against it. Script 1 = Select Device from a drop down list Script 2 = Execute option 1 or 2 against the selected Device They both work indendently but I can't figure out how to intergrate the two. Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $form = New-Object System.Windows.Forms.Form $form.Text = 'Select a Computer' $form.Size = New-Object System.Drawing.Size(300,200) $form.StartPosition = 'CenterScreen' $okButton = New-Object System.Windows.Forms.Button $okButton.Location = New-Object System.Drawing.Point(75,120) $okButton.Size = New-Object System.Drawing.Size(75,23) $okButton.Text = 'OK' $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $okButton $form.Controls.Add($okButton) $cancelButton = New-Object System.Windows.Forms.Button $cancelButton.Location = New-Object System.Drawing.Point(150,120) $cancelButton.Size = New-Object System.Drawing.Size(75,23) $cancelButton.Text = 'Cancel' $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $cancelButton $form.Controls.Add($cancelButton) $label = New-Object System.Windows.Forms.Label $label.Location = New-Object System.Drawing.Point(10,20) $label.Size = New-Object System.Drawing.Size(280,20) $label.Text = 'Please select a computer:' $form.Controls.Add($label) $listBox = New-Object System.Windows.Forms.ListBox $listBox.Location = New-Object System.Drawing.Point(10,40) $listBox.Size = New-Object System.Drawing.Size(260,20) $listBox.Height = 80 [void] $listBox.Items.Add(‘ORTECLAB001’) [void] $listBox.Items.Add('ORTECLAB002’) [void] $listBox.Items.Add('ORTECLAB003’) [void] $listBox.Items.Add('ORTECLAB004’) [void] $listBox.Items.Add('ORTECLAB005') [void] $listBox.Items.Add('ORTECLAB006') [void] $listBox.Items.Add('ORTECLAB007') [void] $listBox.Items.Add('ORTECLAB008') [void] $listBox.Items.Add('ORTECLAB009') [void] $listBox.Items.Add('ORTECLAB010') [void] $listBox.Items.Add('ORTECLAB011') [void] $listBox.Items.Add('ORTECLAB012') [void] $listBox.Items.Add('ORTECLAB013') [void] $listBox.Items.Add('ORTEC00348') $form.Controls.Add($listBox) $form.Topmost = $true $result = $form.ShowDialog() if ($result -eq [System.Windows.Forms.DialogResult]::OK) { $x = \\10.X.X.253\c$\Users\smorgan\Documents\Shutdown.ps1 $x } [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $x=args[0] $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Shutdown | Restart" $objForm.Size = New-Object System.Drawing.Size(300,300) $objForm.StartPosition = "CenterScreen" $objForm.FormBorderStyle = "FixedSingle" $objForm.KeyPreview = $True $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(205,220) $CancelButton.Size = New-Object System.Drawing.Size(75,25) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({$objForm.Close()}) $objForm.Controls.Add($CancelButton) $SButton = New-Object System.Windows.Forms.Button $SButton.Location = New-Object System.Drawing.Size(15,220) $SButton.Size = New-Object System.Drawing.Size(75,25) $SButton.Text = "Shutdown" $SButton.Add_Click({Stop-Computer -Force}) $objForm.Controls.Add($SButton) $RButton = New-Object System.Windows.Forms.Button $RButton.Location = New-Object System.Drawing.Size(110,220) $RButton.Size = New-Object System.Drawing.Size(75,25) $RButton.Text = "Restart" $RButton.Add_Click({Restart-Computer -Force}) $objForm.Controls.Add($RButton) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,20) $objLabel.Size = New-Object System.Drawing.Size(280,120) $objLabel.Text = "Please ensure you have selected the correct LAC/E Box before proceeding." $objForm.Controls.Add($objLabel) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog()2.6KViews1like1CommentPowershell to extract physical memory from Intune joined devices
I found a powershell script that extracts hardware information from Intune joined devices, however, the physicalMemoryInBytes that appears in the output file displays a 0. I've found suggestions on getting it to show the correct memory but they haven't worked. Any ideas?3.7KViews0likes2CommentsNeed to change bulk users Samaccountname to uppercase
Hello I am trying to get this script to update a list of samaccountname from lowercase to uppercase. We have an application that has issues with lowercase. I have tried the below and it is giving me a "You cannot call a method on a null-valued expression" error when running. I am not sure what I am doing wrong but any help will be greatly appreciated. Here is the script I am using now. $users = get-content '.\users.txt' foreach($user in $users){ set-aduser $user -SamAccountName $user.samaccountname.toupper() }Solved5.4KViews0likes4Comments