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
Briantist
Dec 5, 2003

The Professor does not approve of your post.
Lipstick Apathy

GPF posted:

This isn't an answer to anyone's deep question, or a question about some odd thing. This is me being happy.

Due to restrictions on our workstations and servers, I've been locked into the Windows 7/Server 2008 R2 gimped PowerShell 4 for some time. I do tons of automation of DHCP and Printing infrastructure, among other things.

We finally moved our DHCP server to 2012 R2 last week. Today I had the opportunity to experiment. I have tons of PS code full of 'netsh dhcp' commands that pull lists, mangle them to shreds to get the info I need which fires off more 'netsh dhcp' commands. One certain automation piece I wrote is 75-80 lines of code to look for leases in the printer scopes, check to see if they are printers, and, if they're actually printers in a database, turn them into reservations, update that database, and change the port on a print server to the new IP.

After 10 minutes just messing around with 'help dhcp' and doing some minor experimentation, I estimate I'll be able to cut that automation down to approximately 5 lines or less to do the hard parts. And, it'll run faster and with fewer errors.

I'm happy.

The DHCP cmdlets are nice. If you're using the new DHCP failover features just be aware that the Invoke-DhcpServerv4FailoverReplication cmdlet can be subject to kerberos double-hop issues if you try to use it remotely.

Adbot
ADBOT LOVES YOU

Dr. Kayak Paddle
May 10, 2006

GPF posted:

This isn't an answer to anyone's deep question, or a question about some odd thing. This is me being happy.

Due to restrictions on our workstations and servers, I've been locked into the Windows 7/Server 2008 R2 gimped PowerShell 4 for some time. I do tons of automation of DHCP and Printing infrastructure, among other things.

We finally moved our DHCP server to 2012 R2 last week. Today I had the opportunity to experiment. I have tons of PS code full of 'netsh dhcp' commands that pull lists, mangle them to shreds to get the info I need which fires off more 'netsh dhcp' commands. One certain automation piece I wrote is 75-80 lines of code to look for leases in the printer scopes, check to see if they are printers, and, if they're actually printers in a database, turn them into reservations, update that database, and change the port on a print server to the new IP.

After 10 minutes just messing around with 'help dhcp' and doing some minor experimentation, I estimate I'll be able to cut that automation down to approximately 5 lines or less to do the hard parts. And, it'll run faster and with fewer errors.

I'm happy.

I wish I could be happy. This place is stuck with version 2. :suicide:

Dr. Kayak Paddle fucked around with this message at 03:43 on Jan 7, 2016

Hadlock
Nov 9, 2004

Went from an all windows shop hacking powershell about 15% of the time, left the midwest to go work for a Ruby on Rails shop running exclusively on Macs out west.

I've spent 16 hours so far this week hacking powershell at work for a sales demo :toot:

We did manage to draw a line in the sand and officially not support anything lower than Powershell v3, at least. Powershell v2 is not terribly functional once you start getting in to the guts of it.

FISHMANPET
Mar 3, 2007

Sweet 'N Sour
Can't
Melt
Steel Beams
I'm trying to construct the parameters for an exe and executing it from within Powershell. For reference, I'm using CreateMedia.exe in SCCM: https://msdn.microsoft.com/en-us/library/jj155402.aspx

I'm having trouble getting quotation marks to appear in the right places. I'm using the call (&) operator. To start I just manually created the command line and parameters I needed.
code:
& "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CreateMedia.exe" /K:boot /S:"SMS" /L:"Configuration Manager 2012" ...
There's a lot more but that's enough to get the point across. So that works. Now I want to make this more dynamic. Since the call operator doesn't do any interpretation, I need to construct my paramater strings before I call the command.
code:
$cmd = "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CreateMedia.exe"
$typearg = "/K:boot"
$sitecode = "/S:`"SMS`""
$label = "/L:`"Configuration Manager 2012`""
I'm using double quotes and the escape character because I want my strings to contain the double quotes, and some of the variables will be made up of other variables. (If I used single quotes it wouldn't interpret the variables and just take them literally, right?)
So then I use these variables to build the call command.
code:
& $cmd $typearg $sitecode $label
And this is where things fall off the wheels. It appears that the command it passes has the double quotes stripped out, because I get an error:
Invalid parameter: Manager

So, am I constructing this incorrectly? Is there another way I should be doing this?

E: Found this article that includes a tool that will just print the arguments as passed, and it looks like the quotes are preserved but there are some extra ones, so now I'm not entirely sure what's going on.
E2: And I was wrong about not being able to use variables in command parameters, I misinterpreted what I read. So I have very little idea what I'm doing here apparently.

FISHMANPET fucked around with this message at 18:48 on Jan 7, 2016

Roargasm
Oct 21, 2010

Hate to sound sleazy
But tease me
I don't want it if it's that easy
Nah you're on the right track kind of, it's a pain in the rear end and you can use write-host to get around it.

$program = 'quser.exe'
$flag1 = '/server:'
$flag2 = 'localhost'

"& $program $flag1$flag2" won't work, neither will "& $program ($flag1)($flag2)"

The valid syntax for my example uses write-host for var interpretation, like

code:
& $program (write-host "$flag1$flag2")
e: And remember to always use single quotes if the string is supposed to be interpreted literally

Roargasm fucked around with this message at 19:43 on Jan 7, 2016

FISHMANPET
Mar 3, 2007

Sweet 'N Sour
Can't
Melt
Steel Beams
So that appears to pass the string the right way to the showargs.exe, but somehow fails miserably with createmedia.exe. The createmedia.exe logs the full parameter list it's given (sort of, it strips the quotes in the logs) and the parameters it understands, and it apparently wasn't able to read anything from that.
E: Screw it, took my hand constructed string, dropped the variables I actually want to change, and it works just fine.
code:
& $cmd /S:"$sitecode" [etc etc]

FISHMANPET fucked around with this message at 20:12 on Jan 7, 2016

beepsandboops
Jan 28, 2014
I'm trying to write a script to cull old Exchange mailboxes after exporting them, but I can't quite get it to work. I'm also starting to suspect that a. I'm abusing the hell out of piping and could do what I want in a simpler way, and b. There's probably something built in to Exchange anyway.

Here's what I have so far:
code:
$oldMailboxes = Get-Mailbox -OrganizationalUnit "Former Employees" | Get-MailboxStatistics | where {$_.LastLogoffTime -le (Get-Date).AddMonths(-6)} | Select -Property DisplayName | Format-Table -HideTableHeaders | Out-String

$splitLines = $oldMailboxes.Split(“`r`n”)

$mailboxAlias = ($splitLines.Trim() | ForEach-Object {Get-Mailbox -Identity $_ | Select -Property Alias})

$exportJob = ($mailboxAlias | ForEach-Object {New-MailboxExportRequest -Mailbox $_ -Filepath ("\\fileserver\PST\" + $_ + ".pst")})

$exportJob | Wait-Job

$exportedMailboxes = Get-MailboxExportRequest | where {$_.status -eq "Completed"}

$exportedMailboxes | ForEach-Object {Remove-MailboxExportRequest; Disable-Mailbox}
The script gets tripped up when I try and change the array of mailbox display names into separate, trimmed strings to present to Get-Mailbox.

Any tips on how to simplify / do this a different way?

nielsm
Jun 1, 2009



If you're on PowerShell 3 or later, this part:
code:
Select -Property DisplayName | Format-Table -HideTableHeaders | Out-String
is done much simpler like this:
code:
ForEach-Object DisplayName
or, if you don't mind depending on default aliases:
code:
% DisplayName
In PS 3, the ForEach-Object command was extended with an alternate syntax that takes a single property or member function name and just pulls/calls that on each object.

The old version compatible syntax would be:
code:
ForEach-Object { $_.DisplayName }
That similarly pulls out the DisplayName property, and produces an array of string objects, one string for each input object.

Doing that allows you to simplify the following two lines to:
code:
$mailboxAlias = $oldMailboxes | ForEach-Object { (Get-Mailbox -Identity $_).Alias }
However, you can probably do the entire thing much cleaner and safer, to avoid depending on properties like DisplayName, which could theoretically cause multiple matches since it isn't really guaranteed unique. (What if you got a new employee on board who got exactly the same display name as someone who quit 7 months ago?)

How about this?
code:
filter script:Where-MailboxIsOld {
    Where-Object { ($_ | Get-MailboxStatistics).LastLogoffTime -le (Get-Date).AddMonths(-6) }
}

$oldMailboxes = Get-Mailbox -OrganizationalUnit "Former Employees" | Where-MailboxIsOld

$exportJob = $oldMailboxes | ForEach-Object { New-MailboxExportRequest -Mailbox $_.Identity -Filepath ("\\server\PST\" + $_.Alias + ".pst") }

beepsandboops
Jan 28, 2014
Interesting! I didn't know about filter script. I can't find much about it in Microsoft's official documentation though. Is it an alias for something else?

Also this is all in Exchange 2010 shell, which I think corresponds to only Powershell 2, sadly.

Thanks so much for your help!

Swink
Apr 18, 2006
Left Side <--- Many Whelps
2010 SP3 supports installing PS 3.0

nielsm
Jun 1, 2009



beepsandboops posted:

Interesting! I didn't know about filter script. I can't find much about it in Microsoft's official documentation though. Is it an alias for something else?

It's a filter function, placed in the "script" namespace.

The "filter" keyword introduces a filter function. A filter function is pretty much a shortcut to write a full function, that only has a Process block, and implicitly takes a single pipeline input, and produces pipeline output.
They're a simple way to wrap sub-operations you want to either name, or maybe re-use.

The "script:" part is a qualifier that ties to the "Where-MailboxIsOld" name. I'm honestly not entirely sure about it, but it's supposed to place the function in the script namespace, meaning it wouldn't get exported if you load the script as a module, or source it in an interactive session.

The rest of it is effectively a Process block inside a function, meaning the body of the filter function gets executed once for every item in the input pipeline.
Because of that, it should also be possible to write it with an if statement, that then only outputs the mailbox back out if it's sufficiently aged.


Something else is, consider having PS 4 or 5 on your workstation, and then set up a remoting session to your Exchange server. It's much more convenient, lets you work on Exchange through a local PS window, or even use the ISE, without having to open a remote desktop to another machine, or even having the Exchange management tools installed.

nielsm fucked around with this message at 22:28 on Jan 13, 2016

Toshimo
Aug 23, 2012

He's outta line...

But he's right!
Is there a way to perform arithmetic on the results of a regex inside a -replace statement?

Something like:
code:
7 .. 39 | % {Get-ChildItem "*$($($_.toString()).PadLeft(3,'0'))*"} | Rename-Item -NewName { $_.Name -replace '(\d{3})', "$($($($('$1').ToString() - 3).toString()).PadLeft(3,'0'))" }

nielsm
Jun 1, 2009



I don't think there is. Instead, you can try matching the entire filename into three match groups, a prefix, the number, and a suffix, with an if ($x -match "re"). Then use the match groups to reconstruct the new filename and perform the rename.

Venusy
Feb 21, 2007

beepsandboops posted:

Interesting! I didn't know about filter script. I can't find much about it in Microsoft's official documentation though. Is it an alias for something else?

Also this is all in Exchange 2010 shell, which I think corresponds to only Powershell 2, sadly.

Thanks so much for your help!
code:
$session = New-PSSession -ConfigurationName Microsoft.Exchange
Import-PSSession $session
I'm not at work so I can't double check this, but this should allow you to use the Exchange 2010 commands in whichever version of PS you've got installed. Add the -ComputerName parameter if necessary.

Danith
May 20, 2006
I've lurked here for years
In one of my 'make my day easier' scripts it looks like I may need to figure out how to SFTP something from powershell (Well.. i guess I could use a command-line SFTP client but everything from powershell would be better). Has anyone done this/know of sites I can look at?

Is there a .net class I can use? I'm guessing not :(


As long as I'm in here, does anyone recall the changes from Powershell V3 to V5 in regards to handling com objects differently? I installed V5 last week and it really broke my script that was written with V3, where I heavily use IE COM objects. I didn't have the desire to dig far into it but it seemed like instead of waiting for the com operation to complete, it would execute all the operations without waiting for any status back (i.e. $ie = new-object -com internetexplorer.application; $ie.visible = $true; $ie.navigate($site)). With V3, it would open up IE, make it visible, and navigate to the site. With V5, it would make the object, then every other command would just return an exception.. just kinda wondering..

Fenrisulfr
Oct 14, 2012

Danith posted:

In one of my 'make my day easier' scripts it looks like I may need to figure out how to SFTP something from powershell (Well.. i guess I could use a command-line SFTP client but everything from powershell would be better). Has anyone done this/know of sites I can look at?

Is there a .net class I can use? I'm guessing not :(


As long as I'm in here, does anyone recall the changes from Powershell V3 to V5 in regards to handling com objects differently? I installed V5 last week and it really broke my script that was written with V3, where I heavily use IE COM objects. I didn't have the desire to dig far into it but it seemed like instead of waiting for the com operation to complete, it would execute all the operations without waiting for any status back (i.e. $ie = new-object -com internetexplorer.application; $ie.visible = $true; $ie.navigate($site)). With V3, it would open up IE, make it visible, and navigate to the site. With V5, it would make the object, then every other command would just return an exception.. just kinda wondering..

I've not used it myself but you might be able to use WinSCP to do it: https://winscp.net/eng/docs/guide_dotnet. It's not exactly from Powershell but it's the closest I was able to find. I was going to dive into it to automate an SFTP task but since the only manual steps are opening WinSCP, dragging the file over, and closing WinSCP I haven't bothered.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

Danith posted:

In one of my 'make my day easier' scripts it looks like I may need to figure out how to SFTP something from powershell (Well.. i guess I could use a command-line SFTP client but everything from powershell would be better). Has anyone done this/know of sites I can look at?

Is there a .net class I can use? I'm guessing not :(

Fenrisulfr posted:

I've not used it myself but you might be able to use WinSCP to do it: https://winscp.net/eng/docs/guide_dotnet. It's not exactly from Powershell but it's the closest I was able to find. I was going to dive into it to automate an SFTP task but since the only manual steps are opening WinSCP, dragging the file over, and closing WinSCP I haven't bothered.
http://dotps1.github.io/WinSCP/

Mr. Clark2
Sep 17, 2003

Rocco sez: Oh man, what a bummer. Woof.

I'm pretty new to powershell and I'm having some difficulty with a script. I need a script to get just the last 4 digits of the TelephoneNumber field for all users in a specific OU, and then copy them to the ipPhone field. If the telephone number field is empty, do nothing to that entry

Here's what I have so far:

code:
$Users = Get-AdUser  -SearchBase "OU=Test Users,DC=test,DC=local" -Properties *

foreach $user in $Users
   psuedocode here          { Take $_.TelephoneNumber.substring | Set-ADUser  -property ipPhone
Yeah, not much :(
I know what I need to do, it's the how that is giving me fits. Any advice/tips/pointers?

Zaepho
Oct 31, 2013

Mr. Clark2 posted:

I'm pretty new to powershell and I'm having some difficulty with a script. I need a script to get just the last 4 digits of the TelephoneNumber field for all users in a specific OU, and then copy them to the ipPhone field. If the telephone number field is empty, do nothing to that entry

Here's what I have so far:

code:
$Users = Get-AdUser  -SearchBase "OU=Test Users,DC=test,DC=local" -Properties *

foreach $user in $Users
   psuedocode here          { Take $_.TelephoneNumber.substring | Set-ADUser  -property ipPhone
Yeah, not much :(
I know what I need to do, it's the how that is giving me fits. Any advice/tips/pointers?

code:
$Users = Get-AdUser  -SearchBase "OU=Test Users,DC=test,DC=local" -Properties *

foreach ($user in $Users) {
	if ($user.TelephoneNumber -ne '' -and $user.TelephoneNumber -ne $null) {
		$Last4 = $user.TelephneNumber.Substring($($User.TelephoneNumber.Length - 4), 4)
		$set-ADUser -identify $user -property IPPhone -value $Last4
	}
}
Or something along those lines. I'd recommend putting in some progress tracking and some output to the screen so whoever is running it knows it's working/not working.

nielsm
Jun 1, 2009



nm do the above

GPF
Jul 20, 2000

Kidney Buddies
Oven Wrangler

Briantist posted:

The DHCP cmdlets are nice. If you're using the new DHCP failover features just be aware that the Invoke-DhcpServerv4FailoverReplication cmdlet can be subject to kerberos double-hop issues if you try to use it remotely.

That is damned good to know about. We are going to use the new load-balancing/failover setup very soon, so I'm actually going to send myself an email so I won't forget about it.

ErIog
Jul 11, 2001

:nsacloud:
So this is not necessarily a PowerShell question, but it is related.

I have a simple script I want to run every 15 minutes to make sure our digital signage is running off the most recent files. Everything's going great except for taskeng.exe crapping up the display every time my script runs. I've been loving with it for a few hours and nothing works, and at this point I'm ready to just go use something else like CygWin's cron unless there's some simple thing I'm missing.

Hadlock
Nov 9, 2004

I've never heard of task scheduler interfering with other processes/video

If it's giving you that much grief, try something else?

I personally hate task scheduler but it's dead reliable at what it does, so I'm thinking your issue lies elsewhere.

ErIog
Jul 11, 2001

:nsacloud:
Turns out you're right and it was a dumb thing I was doing in my script that was causing the issue.

Mr. Clark2
Sep 17, 2003

Rocco sez: Oh man, what a bummer. Woof.

Zaepho posted:

code:
$Users = Get-AdUser  -SearchBase "OU=Test Users,DC=test,DC=local" -Properties *

foreach ($user in $Users) {
	if ($user.TelephoneNumber -ne '' -and $user.TelephoneNumber -ne $null) {
		$Last4 = $user.TelephneNumber.Substring($($User.TelephoneNumber.Length - 4), 4)
		$set-ADUser -identify $user -property IPPhone -value $Last4
	}
}
Or something along those lines. I'd recommend putting in some progress tracking and some output to the screen so whoever is running it knows it's working/not working.

Thanks! You got me about 98% of the way there, just had to modify the last line slightly.

PBS
Sep 21, 2015

Mr. Clark2 posted:

Thanks! You got me about 98% of the way there, just had to modify the last line slightly.

Depending on the size of that OU you probably don't want to do -properties *, -properties TelephoneNumber should work fine.

Sidenote, does having just -identity $user work? I always specify $user.SamAccountName. I'll have to test it when I get back to work.

Venusy
Feb 21, 2007
Yeah, the AD cmdlets will accept (relevant) AD objects for identity, as well as SamAccountName, SID, DistinguishedName, etc. The only time you really need to be careful is when passing them to other things - I usually need to use $user.UserPrincipalName when passing output filtered in AD to Exchange 2007 commands.

Irritated Goat
Mar 12, 2005

This post is pathetic.

Venusy posted:

Yeah, the AD cmdlets will accept (relevant) AD objects for identity, as well as SamAccountName, SID, DistinguishedName, etc. The only time you really need to be careful is when passing them to other things - I usually need to use $user.UserPrincipalName when passing output filtered in AD to Exchange 2007 commands.

I abuse the poo poo out of SamAccountName for any O365 stuff. I can't think of anything I've had to not use it on actually..

MF_James
May 8, 2008
I CANNOT HANDLE BEING CALLED OUT ON MY DUMBASS OPINIONS ABOUT ANTI-VIRUS AND SECURITY. I REALLY LIKE TO THINK THAT I KNOW THINGS HERE

INSTEAD I AM GOING TO WHINE ABOUT IT IN OTHER THREADS SO MY OPINION CAN FEEL VALIDATED IN AN ECHO CHAMBER I LIKE

I've got a quick question regarding PS, because I'm dumb and haven't used it much.

I'm looking to modify some NIC settings, specifically here I'm trying to disable VMQ on a few different adapters, and I'm not sure what the issue is, whether it's syntax or perhaps I'm using a cmdlet that isn't available

Let's say my adapter name is: The Network Adapter


code:
Set-NetAdapterVMQ -name 'The Network Adapter' -enable $false
Now when I use -whatif it just outputs the command I'm typing back at me like this: What if: Set-NetAdapterVMQ -name 'The Network Adapter' -enable $false


Generally other cmdlets I've used, when I add -whatif, it actually tells me what is going to happen. So I don't know if the issue is syntax related (perhaps the way I'm presenting the name of the adapter is incorrect) or if the cmdlet is not available to me for :reasons:

Running Server 2012 R2 with powershell major 4, build -1 revision -1... according to $host.version.

MF_James fucked around with this message at 22:06 on Feb 4, 2016

FISHMANPET
Mar 3, 2007

Sweet 'N Sour
Can't
Melt
Steel Beams
I think the author of the cmdlet has to write in support for -whatif, so that command just does it poorly. Are you having actual roubles executing the command, and/or is there a reason you're not using Disable-NetAdapterVmq?

MF_James
May 8, 2008
I CANNOT HANDLE BEING CALLED OUT ON MY DUMBASS OPINIONS ABOUT ANTI-VIRUS AND SECURITY. I REALLY LIKE TO THINK THAT I KNOW THINGS HERE

INSTEAD I AM GOING TO WHINE ABOUT IT IN OTHER THREADS SO MY OPINION CAN FEEL VALIDATED IN AN ECHO CHAMBER I LIKE

FISHMANPET posted:

I think the author of the cmdlet has to write in support for -whatif, so that command just does it poorly. Are you having actual roubles executing the command, and/or is there a reason you're not using Disable-NetAdapterVmq?

I could use that one. I'm not having troubles, except with "testing" the cmdlet to make sure it will run, these commands are getting compiled into a larger script so I can do logging and run it on multiple machiens with (hopefully) the same results without me typing out 20-30 different commands across all the machines.

nielsm
Jun 1, 2009



FISHMANPET posted:

I think the author of the cmdlet has to write in support for -whatif, so that command just does it poorly. Are you having actual roubles executing the command, and/or is there a reason you're not using Disable-NetAdapterVmq?

Do that if it's all you need to do.

Apart from that, the parameter is called "-enabled".

I think -WhatIf is dependent on the implementation having confirmation checks. In WhatIf mode it'd then just print the step to be confirmed, and continue as if the user rejected.

nielsm fucked around with this message at 22:35 on Feb 4, 2016

MF_James
May 8, 2008
I CANNOT HANDLE BEING CALLED OUT ON MY DUMBASS OPINIONS ABOUT ANTI-VIRUS AND SECURITY. I REALLY LIKE TO THINK THAT I KNOW THINGS HERE

INSTEAD I AM GOING TO WHINE ABOUT IT IN OTHER THREADS SO MY OPINION CAN FEEL VALIDATED IN AN ECHO CHAMBER I LIKE

Thanks for the info guys, I have "one" more question, we are setting up our 6 hyper-V hosts to handle anti-affinity, I've essentially shamelessly ripped this from another site, while changing some info (site here: https://robertsmit.wordpress.com/2014/02/14/separate-vm-in-hyper-v-virtual-machines-using-anti-affinity-winserv-hyperv-drs/)


code:
		$SQLAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$SQLAntiAffinity.Add(“SQL Server Instances”)

		$DMCAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$DMCAntiAffinity.Add(“Domain Controllers”)

		$FILEAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$FILEAntiAffinity.Add(“File Servers”)

		$QASQLAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$QASQLAntiAffinity.Add(“QA SQL Server Instances”)

		$QADCAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$QADCAntiAffinity.Add(“QA Domain Controllers”)

		$QAFILEAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$QAFILEAntiAffinity.Add(“QA File Servers”)

		(Get-ClusterGroup –Name SQL1-LOCATION-DOMAIN).AntiAffinityClassNames = $SQLAntiAffinity
		(Get-ClusterGroup –Name SQL2-LOCATION-DOMAIN).AntiAffinityClassNames = $SQLAntiAffinity

		(Get-ClusterGroup –Name FILE1-LOCATION-DOMAIN).AntiAffinityClassNames = $FILEAntiAffinity
		(Get-ClusterGroup –Name FILE2-LOCATION-DOMAIN).AntiAffinityClassNames = $FILEAntiAffinity

		(Get-ClusterGroup –Name DMC1-LOCATION-DOMAIN).AntiAffinityClassNames = $DMCAntiAffinity
		(Get-ClusterGroup –Name DMC2-LOCATION-DOMAIN).AntiAffinityClassNames = $DMCAntiAffinity

		(Get-ClusterGroup –Name SQL1-LOCATION-QADOMAIN).AntiAffinityClassNames = $QASQLAntiAffinity
		(Get-ClusterGroup –Name SQL2-LOCATION-QADOMAIN).AntiAffinityClassNames = $QASQLAntiAffinity

		(Get-ClusterGroup –Name FILE1-LOCATION-QADOMAIN).AntiAffinityClassNames = $QAFILEAntiAffinity
		(Get-ClusterGroup –Name FILE2-LOCATION-QADOMAIN).AntiAffinityClassNames = $QAFILEAntiAffinity

		(Get-ClusterGroup –Name DMC1-LOCATION-QADOMAIN).AntiAffinityClassNames = $QADMCAntiAffinity
		(Get-ClusterGroup –Name DMC2-LOCATION-QADOMAIN).AntiAffinityClassNames = $QADMCAntiAffinity
Am I understanding/using this correctly? These somewhat confuse me:
code:
$SQLAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$SQLAntiAffinity.Add(“SQL Server Instances”)
The top portion looks like we're creating a variable that will contain the system.collections.specialized.stringcollection, but I'm not understanding the second line (perhaps because I'm not fully understanding the first line), the second line seems to be attaching a descriptor to the $SQLAntiAffinity variable that I created.

MF_James fucked around with this message at 00:13 on Feb 5, 2016

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

MF_James posted:

Thanks for the info guys, I have "one" more question, we are setting up our 6 hyper-V hosts to handle anti-affinity, I've essentially shamelessly ripped this from another site, while changing some info (site here: https://robertsmit.wordpress.com/2014/02/14/separate-vm-in-hyper-v-virtual-machines-using-anti-affinity-winserv-hyperv-drs/)


code:
		$SQLAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$SQLAntiAffinity.Add(“SQL Server Instances”)

		$DMCAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$DMCAntiAffinity.Add(“Domain Controllers”)

		$FILEAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$FILEAntiAffinity.Add(“File Servers”)

		$QASQLAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$QASQLAntiAffinity.Add(“QA SQL Server Instances”)

		$QADCAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$QADCAntiAffinity.Add(“QA Domain Controllers”)

		$QAFILEAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$QAFILEAntiAffinity.Add(“QA File Servers”)

		(Get-ClusterGroup –Name SQL1-LOCATION-DOMAIN).AntiAffinityClassNames = $SQLAntiAffinity
		(Get-ClusterGroup –Name SQL2-LOCATION-DOMAIN).AntiAffinityClassNames = $SQLAntiAffinity

		(Get-ClusterGroup –Name FILE1-LOCATION-DOMAIN).AntiAffinityClassNames = $FILEAntiAffinity
		(Get-ClusterGroup –Name FILE2-LOCATION-DOMAIN).AntiAffinityClassNames = $FILEAntiAffinity

		(Get-ClusterGroup –Name DMC1-LOCATION-DOMAIN).AntiAffinityClassNames = $DMCAntiAffinity
		(Get-ClusterGroup –Name DMC2-LOCATION-DOMAIN).AntiAffinityClassNames = $DMCAntiAffinity

		(Get-ClusterGroup –Name SQL1-LOCATION-QADOMAIN).AntiAffinityClassNames = $QASQLAntiAffinity
		(Get-ClusterGroup –Name SQL2-LOCATION-QADOMAIN).AntiAffinityClassNames = $QASQLAntiAffinity

		(Get-ClusterGroup –Name FILE1-LOCATION-QADOMAIN).AntiAffinityClassNames = $QAFILEAntiAffinity
		(Get-ClusterGroup –Name FILE2-LOCATION-QADOMAIN).AntiAffinityClassNames = $QAFILEAntiAffinity

		(Get-ClusterGroup –Name DMC1-LOCATION-QADOMAIN).AntiAffinityClassNames = $QADMCAntiAffinity
		(Get-ClusterGroup –Name DMC2-LOCATION-QADOMAIN).AntiAffinityClassNames = $QADMCAntiAffinity
Am I understanding/using this correctly? These somewhat confuse me:
code:
$SQLAntiAffinity = New-Object System.Collections.Specialized.StringCollection
		$SQLAntiAffinity.Add(“SQL Server Instances”)
The top portion looks like we're creating a variable that will contain the system.collections.specialized.stringcollection, but I'm not understanding the second line (perhaps because I'm not fully understanding the first line), the second line seems to be attaching a descriptor to the $SQLAntiAffinity variable that I created.

It's creating a collection and adding a single item to the collection.

Dr. Arbitrary
Mar 15, 2006

Bleak Gremlin
I just started doing some work for a programmer who has a big problem with if statements. I don't know the whole deal yet, but I assume it's a normal thing for some kinds of coding.

Are there good ways to avoid IF with Powershell or is the language just not built for that?

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Dr. Arbitrary posted:

I just started doing some work for a programmer who has a big problem with if statements. I don't know the whole deal yet, but I assume it's a normal thing for some kinds of coding.

Are there good ways to avoid IF with Powershell or is the language just not built for that?

Does he have a background in functional programming? PowerShell isn't a functional language, don't try to shoehorn it into that mold. I mean, yeah, you can avoid writing if statements if you like unreadable code.

Dr. Arbitrary
Mar 15, 2006

Bleak Gremlin
Is Ruby functional programming? If so, I think we've cracked the case.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Dr. Arbitrary posted:

Is Ruby functional programming? If so, I think we've cracked the case.

It isn't.

Dr. Arbitrary
Mar 15, 2006

Bleak Gremlin
Well... I guess I've got to set up a meeting to figure out what this guy is all about. Either he's really good or really bad. I know that I don't know enough to tell the difference.

Either way I'll learn something!

Adbot
ADBOT LOVES YOU

Dr. Arbitrary
Mar 15, 2006

Bleak Gremlin
Ok. Had a quick meeting and figured out his deal.

IF statements are great for things like
pre:
if ($DiskSpaceUsed -gt $AlarmThreshold) { $Alarm = $True}|
But suppose we're writing a program to write firewall rules.

pre:
if ($Serverclass -eq "Win2008db") {
Apply Rule 1A
Apply Rule 2A
}
elseif ($Serverclass -eq "Win20012db") {
Apply Rule 1B
Apply Rule 2B
}
elseif ($Serverclass -eq "Win2008app") {
Apply Rule 1C
Apply Rule 2C
}
and so on
would be bad, because every time you need to add a new type of server, you have to rewrite the code, and if you change the rules, you have to rewrite the code.
Instead, you'd want to do something like create a simple program that queries rules.xml and servertypes.xml or something.

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