Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
The Fool
Oct 16, 2003


So you have a hashtable like this?
code:
$hashtable = @{ "array1" = 'this','is','array','1'; "array2" = 'hello','array','2' }
$hashtable

Name                           Value
----                           -----
array1                         {this, is, array, 1}
array2                         {hello, array, 2}


What's wrong with just doing this?
code:
$array1 = $hashtable.array1
$array1

this
is
array
1

Or am I missing something?

Adbot
ADBOT LOVES YOU

The Fool
Oct 16, 2003


A single command won't do it, but two commands piped together will

code:
Get-Mailbox | Get-RecipientPermission -Trustee $user
This queries all mailboxes when run, so it can take a minute. If you need to do this for a bunch of users at once, you should cache the get-mailbox results and loop over that.

Also, if you have more than 1000 mailboxes, you will want to specify ResultSize

https://docs.microsoft.com/en-us/powershell/module/exchange/get-mailbox?view=exchange-ps
https://docs.microsoft.com/en-us/powershell/module/exchange/get-recipientpermission?view=exchange-ps

The Fool
Oct 16, 2003


Get-RecipientPermission should only work for SendAs permissions.

If you are checking for other permissions, you would use Get-MailboxPermission

The Fool
Oct 16, 2003


I suppose I assumed and did not ask.

Get-RecipientPermission is O365 only and will not work with on-prem exchange.

The Fool
Oct 16, 2003


A quick google found me this: https://www.beaming.co.uk/knowledge-base/powershell-commands-view-mailbox-permissions-migrating-exchange-server/

Where you need to query ad extended permissions to get what you're looking for.

Not something I've ever tried to do though.

The Fool
Oct 16, 2003


Irritated Goat posted:

I'm trying to make my scripts more "sharable" in the sense that it isn't calling for a specific named server\ip address. If I was doing somthing like

code:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri [url]http://exchange.domain.com/powershell[/url] -Authentication Kerberos -Credential $UserCredential 

I want to be able to share it regardless of if that Exchange server croaks, it still works with the new one.

The first solution I can thought of is to setup a cname for your script to point to, but then someone needs to maintain the cname record.

DNS load balancer might work too.

Another solution would be to maintain a list of servers in a csv or json file, then reference that in your scripts.

Or you could of course just make the person running your script specify the server at run time.


quote:

After that, I get to figure out the best way to deal with timeouts on VPN for AD queries cause :sigh:

you can control timeout values in ad, otherwise reduce the size of the dataset you're querying in one go. I think most of the get-ad cmdlets support pagesize

The Fool
Oct 16, 2003


Yes.

Get-Aduser to get your list of users, iterate through, then set-aduser.
Iirc, the manager field needs to be a distinguished name or an ad object

The Fool
Oct 16, 2003


MJP posted:

Got what is probably a dumb variable passing question.

I have a CSV. It has two columns: subscriptionName and annualCost.

I need to run
code:
New-AzSubscriptionDeployment -Templatefile foo.json -subscriptionName %name of subscription goes here% -annualCost %annual cost for the subscription goes here%
once for each subscription in subscriptionName.

Is that just a matter of something like this?
code:
import-csv subscriptionFile.csv
foreach ($subscriptionName in $subscriptionNames$){New-AzSubscriptionDeployment -Templatefile foo.json -subscriptionName $subscriptionName -annualCost $annualcost}

more like this:

code:
$subscriptionFile = import-csv subscriptionFile.csv
foreach ($row in $subscriptionFile) {
    New-AzSubscriptionDeployment -Templatefile foo.json -subscriptionName $row.subscriptionName -annualCost $row.annualcost
}
You need to import your csv into a variable or use the pipeline. I prefer the variable route as it's more readable and easier to manage.

ForEach will iterate over your file object, processing each row. You access the columns as properties of the row object.

The Fool fucked around with this message at 17:18 on Nov 19, 2020

The Fool
Oct 16, 2003


MJP posted:

Thanks tons! Just realized I now have to do this per subscription, and give them unique names, so now I need to run select-azsubscription -subscriptionName %subscription name goes here% and then run the new-AzSubscriptionDeployment script D:

Am I looking at this, then?
code:
$subscriptionFile = import-csv subscriptionFile.csv
foreach ($row in $subscriptionFile) {(Select-AzSubscription -subscriptionName $row.subscriptionName)

    New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $row.subscriptionName"_Annual" -annualCost $row.annualcost
}

Not testing this, but I don't think inlining the string works in that situation.

You could do this:
code:
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $row.subscriptionName + "_Annual" -annualCost $row.annualcost
or:
code:
$budgetName = $row.subscriptionName + "_Annual"
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName $budgetName -annualCost $row.annualcost

The Fool
Oct 16, 2003


MJP posted:

No luck - + isn't accepted as a positional parameter for New-AzSubscriptionDeployment. It looks like you may indeed be right about inlining.

I was able to at least concatenate the names and put them into an additional column in the CSV - I'll have to futz to get it into our test environment but that might work around the issue.

Thanks for taking the time to help!

Actually, mystes is right and I forgot parentheses in that example

code:
New-AzSubscriptionDeployment -Templatefile foo.json -budgetName ($row.subscriptionName + "_Annual") -annualCost $row.annualcost

The Fool
Oct 16, 2003


Powershell should only unpack single element (and zero element) arrays in the pipeline.

If you pass your array as an argument you shouldn’t have this problem.

The Fool
Oct 16, 2003


I mean, it works but will create an object array where each element is a line of the text results of the command

The Fool
Oct 16, 2003


Get stuff organized into repos ASAP. If your org is already on bitbucket it’s probably fine but Azure devops is free for 5 users and you’ll get some lightweight project management, repos, and pipelines.

Azure devops pipelines work amazingly well as task runners.

The Fool
Oct 16, 2003


Iirc there some potential problems with a max process count


E: check MaxProcessesPerShell and see if you’re hitting it

The Fool
Oct 16, 2003


Of you absolutely must solve this problem yourself with code use Python+pandas instead of powershell

There might be a higher learning curve if you’ve never used it before but it is purpose built for solving these kinds of problems

The Fool
Oct 16, 2003


bare bones phone posting:

code:
param($department)

$fileLookup = @{
  shipping = "file01"
  legal = "file02"
}

& app.exe -dept $department -file $fileLookup[$department]

The Fool
Oct 16, 2003


it's called a hashtable

The Fool
Oct 16, 2003


Of the top of my head I see two issues:

-match works on strings, $payload is an array of strings

$Matches isn't getting set anywhere, so I'm not sure what you expect its value to be.

The Fool
Oct 16, 2003


To solve the first issue, I would do something like this:
code:
ForEach ($line in $payload) {$line -match '(?<dollars>\d+)\.(?<cents>\d+)'}

The Fool
Oct 16, 2003


the naive way to do it would be to increment a marker variable while iterating through your list of items

when that marker variable hits your intended batch size, send it, then reset and repeat until you finish your list

I can throw up an example in a little bit

There are more clever ways to do batching, but this will do what you need

The Fool
Oct 16, 2003


Secure strings are their own object type and you can read it from the user using Get-Credential

The Fool
Oct 16, 2003


Not powershell, but the report described here might tell you what you want: https://learn.microsoft.com/en-us/exchange/monitoring/mail-flow-reports/mfr-auto-forwarded-messages-report

Adbot
ADBOT LOVES YOU

The Fool
Oct 16, 2003


Does Get-History not provide enough info?

If you're wanting to get a log of commands run in a script, you can run Get-History | Export-CSV at the end of it and it should contain all of the info for commands run in that session.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply