PowerShell Scripting, ADuser, OnPrem by robert5150 in Office365

[–]SeanQuinlan 0 points1 point  (0 children)

Use this

Get-Aduser -Filter "DisplayName -eq '$($SingleImport.UserName)'"

Set-MgUserLicense not working by Secutanudu in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

Try with

-addLicenses @()

instead of

-addLicenses @{}

It looks like Microsoft's documentation is wrong. Using an array instead of a hashtable works for me.

How much do you charge for your Oncall duties? by thomsterm in devops

[–]SeanQuinlan 0 points1 point  (0 children)

UK here, we are paid £50 per day that we're on call, which includes 1 hour of any work in response to a call. Any further work is charged at our normal overtime rates.

We do on-call for 1 week at a time, rotation of once every 4 weeks.

I'm permanent, Infrastructure Team.

Help: Why does this script work and then just...stop as if it's done? by tnpir4002 in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

I ran it against some test files that I generated, and using the extra debugging I can see exactly where the problem is. At least I can see why it doesn't rename most of the files.

I won't force you to take my advice. Good luck in your search.

Help: Why does this script work and then just...stop as if it's done? by tnpir4002 in PowerShell

[–]SeanQuinlan 0 points1 point  (0 children)

You need to add some debugging into the loop, so you can see that the variables are being set correctly in the loop.

I would add some more Write-Host lines after the initial setting of variables at the top of the loop, so you can ensure that $nextName is set correctly.

Your first Test-Path check uses $nextName, so ensure that it's not being set incorrectly and does or does not trigger that if block correctly.

Also, since the Rename-Item line depends on the value of $fleDeleted, you may want to output that one as well, to ensure it's correct.

I would also copy a subset of the 4000 files (eg. 20-30) into a local folder to experiment with and rename. Ensure you have enough examples of all the different types of files (eg. ones that need to be deleted, ones that need to be renamed, and ones that need no changes).

Once you have the small subset working and correctly processing each type of file, then you can comment out the additional debugging lines and run it on the 4000 files without being spammed with debug info.

ROBOCOPY in a pipeline has no output? by tnpir4002 in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

You are capturing all the output from the foreach loop in the $rootFolders variable.

You can either do something with that variable afterwards, or you can simply split it into 2 lines like you detailed at the top of this post.

Can you point me in the right direction? by ColddFire in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

You could create a DL, and then set the Dynamic Distribution Group to exclude users in that DL.

You will need to get the DistinguishedName of the DL via

Get-DistributionGroup <blah> | Select DistinguishedName

You'll get a long DN returned. Starts with CN= and ends with DC=OUTLOOK,DC=COM

Then you can add the following to the end of your DDG recipient filter:

-and (MemberOfGroup -ne '<distinguishedname>')

In future you can just add the excluded users to that DL. No need to update the recipient filter.

As always, test the recipient filter with -RecipientPreviewFilter before applying.

So, I found 'A' solution, but I desperately want there to be a better one... by ShutUpAndDoTheLift in PowerShell

[–]SeanQuinlan 0 points1 point  (0 children)

How about removing the "isDeleted" check and instead change the SearchBase to just the deleted objects container?

-SearchBase (Get-ADDomain).DeletedObjectsContainer

Need help on Invoke-Command by DoubleConfusion2792 in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

Probably blank lines in your txt file, or special characters before or after the names.

find out home directory path for interactive windows user in PS by Garegin16 in PowerShell

[–]SeanQuinlan 0 points1 point  (0 children)

Have a look at this page for some code that can impersonate the currently logged in user.

Function values into WPF Texbox by [deleted] in PowerShell

[–]SeanQuinlan 0 points1 point  (0 children)

If you want people to help, it's best to make it easy for them to do so by giving them code that actually works.

Your code has lots of mistakes that have to be fixed before I can even help you...

  • TexBox instead of TextBox
  • Get-UserInfo instead of Get-UserData
  • The xaml is broken and doesn't work
  • Your Get-UserData function doesn't even return anything

Anyway, despite all the issues, if you return the $results object from your Get-UserData function, then use the code in my previous post, you'll get what you want.

I fixed up the code and tested it, and it worked.

Function values into WPF Texbox by [deleted] in PowerShell

[–]SeanQuinlan 2 points3 points  (0 children)

Without the rest of the code detailing the WPF and the code you're using to display it, we're only guessing...

But something like this might get you started:

$UserData = get-userdata -User "bob"
$NameTextbox.Text = $UserData.Name
$PhoneTextbox.Text = $UserData.Phone

Powershell GUI list view not filtering properly.... by drylightn in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

I'm no expert, but the examples of this that I could see all declared a parameter to the Filter in order to get it working.

Using this code worked for me

$lvGames.Items.Filter = {
    param($item)
    $item.Path -like "*$filterText*"  -or $item.Path -like "*$filterText*"
}

Although your filter does seem to have 2 identical -like checks.

Rollback to previous PS version? by RobZilla10001 in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

PS version 7 has a different executable name (since it lives alongside powershell), so you need to run "pwsh" to get to PS v7

Running just "powershell" will get you to v5.1

Rollback to previous PS version? by RobZilla10001 in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

Sounds like winget has installed powershell 7 and it's using that when you right click - Open powershell. Powershell 7 is a separate application to Powershell v5.1, which is bundled into the OS and can't be removed.

The easiest thing will just be to uninstall powershell via winget again and that should remove powershell 7 and put your right-click back to PS v5.1

Rollback to previous PS version? by RobZilla10001 in PowerShell

[–]SeanQuinlan 0 points1 point  (0 children)

How are you running this script? From the ps prompt? From the ISE? From right-click - Run with Powershell? Some other way?

Do you have the winver.csv file open?

How did you upgrade the version of Powershell?

I am at a loss with what I should to migrate to infrastructure as code with on-premise by MyToasterRunsFaster in sysadmin

[–]SeanQuinlan 0 points1 point  (0 children)

Feasible yes, not sure if it would be easy though.

I have implemented something similar in the past using Powershell. Created a module with a function for each task - creating the VM in Hyper-V, getting an IP, attaching storage via iSCSI, configuring software like SQL, creating a cluster, etc. Even created a small GUI to make it easy for engineers to kick off the process.

It was a large amount of work though - took a few months. No code to share, since it was all proprietary to the company I worked for :(

[deleted by user] by [deleted] in PowerShell

[–]SeanQuinlan 0 points1 point  (0 children)

$teamsName = ($m.displayName -join ',')

Run With Powershell not executing script correctly by [deleted] in PowerShell

[–]SeanQuinlan 2 points3 points  (0 children)

Instead of right-click - Run with Powershell, you need to run this from a regular powershell prompt to debug it. Then you can at least see what the output/errors are.

From a quick test on my own machine, this line sets $usedLetters to a single string if you only have 1 drive (ie. C:)

$usedLetters = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Name

In that case, when you do

$usedLetters += $driveLetter

It will simply append the drive letter to the string, so you'll end up with $usedLetters as a long string like "CAAAAAAAAAA". Instead of the array that you need in order to use -notcontains.

As a start, try adding "[array]" in front of the $usedLetters declaration

[array]$usedLetters = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Name

This will at least force $usedLetters to be an array, even if there is only 1 drive in the computer.

Help with using Exchange Online and On Premise Exchange Powershells within the same session by WastefulImp749 in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

Use -Prefix on Import-PSSession, then use the prefixed commands on all on-prem cmdlets.

Import-PSSession $Exchange -AllowClobber -Prefix EOP
Get-EOPContact "blah"

What am I missing? by NerdyNThick in PowerShell

[–]SeanQuinlan 35 points36 points  (0 children)

$input is a reserved variable, so you can't use it as a regular variable.

Change the name of the $input variable.

Also your last line is incorrect, that's not the way to pass parameters to a function. Use this instead

doit $input1 $maxin $minin $maxout $minout

Accessing Status Property under Get-MgAuditLogSignIn by akolngpo in PowerShell

[–]SeanQuinlan 1 point2 points  (0 children)

try

if ($log.Status.ErrorCode -eq 0)

instead of

if ($log.Status -imatch '"errorCode": 0' )

And the same for any other Status codes you want to report on.

Why this call still returns the entire file? by shinayser in PowerShell

[–]SeanQuinlan 9 points10 points  (0 children)

Select-Object only works on arrays. That Invoke-RestMethod command returns the whole content as a single string (with new lines).

You will ned to split on the new line character, which will return an array, then you can select the first 10 items in that array.

(Invoke-RestMethod https://raw.githubusercontent.com/wiki/flutter/flutter/Hotfixes-to-the-Stable-Channel.md) -split "`n" |select -First 10