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
psylent
Nov 29, 2000

Pillbug

adaz posted:

code:
$users = Get-ADUser -filter * -SearchBase "OU=YourDeleteOU,DC=Your,DC=Domain,DC=Com"

foreach($user in $users) {
   $deObj = [ADSI]"LDAP://$($user.distinguishedName)" 
   foreach($group in $deObj.MemberOf) {
       Remove-ADGroupMember -identity $group -members $user.name 
  }
 
}
DO you have like a spreadsheet or text file of the users? You can automate the moving to a different OU as well with that.
Thanks so much for this, really appreciated.

As it turns out we're running Exch2010 here, but our DCs are still running bloody 2003 so I can't run the script. :(

Adbot
ADBOT LOVES YOU

adaz
Mar 7, 2009

psylent posted:

Thanks so much for this, really appreciated.

As it turns out we're running Exch2010 here, but our DCs are still running bloody 2003 so I can't run the script. :(


Didn't realize the cmd-lets required 2008 R2 to be running on the servers or that functionality level... but even if they do you can still run the script just need to use the root .NET classes :toot: (see this is why I don't trust the dumb modules, they are never in the place you need them)


code:
$root = [adsi]"LDAP://ou=your,ou=deletedou,dc=domain,dc=com"
$searcher = New-Object DirectoryServices.DirectorySearcher $root
$searcher.Filter = "(objectClass=user)"
$users = $searcher.FindAll()

foreach($user in $users) {
   $deObj = $user.GetDirectoryEntry()
   foreach($group in $deObj.MemberOf) {
      $deGroup = New-Object DirectoryServices.DirectoryEntry("LDAP://$group")
      $deGroup.remove("LDAP://$($deObj.distinguishedname)")
      $deGroup.SetInfo()
  }
}

adaz fucked around with this message at 00:28 on Dec 21, 2011

Moey
Oct 22, 2010

I LIKE TO MOVE IT
Looking for a quick pointer in the right direction. Had some success last night using powershell to do some bulk file renaming at home. Get into work today, now I actually get to try and use some of that!

I would like to try and hack through alot of this on my own, but currently I am trying to take a directory with some files in it, then go through and create a folder in that directory with the name of each file name.

Example would be I currently have c:\ps which contains a.txt b.txt c.txt

I need to then create 3 folders, c:\ps\a, c:\ps\b, c:\ps\d

Once that is created, I need to move the respective txt files into the respective folders.

Currently hung on creating the folders by the file names. I was trying something along the lines of

code:
gci |new-item -name $_.Name -itemtype "directory"
But that is complaining that c:\ps already exists.

Wicaeed
Feb 8, 2005
More loving noobie questions:

I am rewriting a vbscript script that we currently use to work around the horrible Windows Server 2008/R2 backup feature.

Right now part of the vbscript looks as such:

code:
 
Sub get2K3DBackupStatus
	If objFSO.FolderExists(BackupFolder2K3D) Then
		Set BackupFolder2K3DTmp = objFSO.GetFolder(BackupFolder2K3D)
		BackupFolder2K3DInfo = "Drive backed up on " & BackupFolder2K3DTmp.DateLastModified & "." & " Total Size: " & BackupFolder2K3DTmp.Size /1024\1024 & "MB"
		objFSO.MoveFolder BackupFolder2K3D, "D:\DriveD_" & CurrentDate
		objFSO.CreateFolder "S:\Backup\BackupD_" & CurrentDate
		objFSO.CopyFolder "D:\DriveD_" & CurrentDate , "S:\DriveD" & CurrentDate
	Else
		BackupFolder2K3DInfo = "Drive backup does not exist"
	End If
End Sub
I am trying to rewrite that into a powershell function, so far I have the following. I've already defined a few variables that this function relies on, and I can use the test-path cmdlet to make sure that the results they are returning are true, however the boolean operator in this function always thinks that the path I'm looking for doesn't exist, weather or not I'm using a variable.

code:
Function get2k3DBackupStatus
{
If ((Test-Path "C:\dell") -ne $false)
{$BackupFolder2K3DInfo = "Drive backed up on $CurrentDate"}
Else
{$BackupFolder2K3DInfo = "Drive backup does not exists"}
}	
Write-Host $BackupFolder2K3DInfo
Always returns "Drive backup does not exists". Is there some secret I'm missing to using variables and boolean operators in functions?

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!

Moey posted:

Looking for a quick pointer in the right direction. Had some success last night using powershell to do some bulk file renaming at home. Get into work today, now I actually get to try and use some of that!

I would like to try and hack through alot of this on my own, but currently I am trying to take a directory with some files in it, then go through and create a folder in that directory with the name of each file name.

Example would be I currently have c:\ps which contains a.txt b.txt c.txt

I need to then create 3 folders, c:\ps\a, c:\ps\b, c:\ps\d

Once that is created, I need to move the respective txt files into the respective folders.

Currently hung on creating the folders by the file names. I was trying something along the lines of

code:
gci |new-item -name $_.Name -itemtype "directory"
But that is complaining that c:\ps already exists.

Well, when creating a folder manually in explorer with the same name as a file, it asks me if I want to overwrite. You could chop off the extension (or add a .dir extension or something) to differentiate the name.

adaz
Mar 7, 2009

Moey posted:

code:
gci |new-item -name $_.Name -itemtype "directory"
But that is complaining that c:\ps already exists.

You were very close -- just need the basename (which omits the file extension) and to put the piepline input in quotes so powershell expands it before attempting to pass it to the parameter - which results in a null being passed and the new-item trying to create the root directory over again

code:
gci | new-Item -name "$_.basename" -itemtype "directory"

adaz fucked around with this message at 16:34 on Dec 21, 2011

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!

Wicaeed posted:

Always returns "Drive backup does not exists". Is there some secret I'm missing to using variables and boolean operators in functions?

This is a problem of scope, your Variable gets set inside the function, but doesn't exist outside it. You could try three things: you could move the Write-Host inside the function, you could not use a function, but have it in the main executing code, or you could have the function return it:

code:
Function get2k3DBackupStatus
 {
 If ((Test-Path "C:\dell") -ne $false)
  {return "Drive backed up on $CurrentDate"}
 Else
  {return "Drive backup does not exists"}
 }

$BackupFolder2K3DInfo = get2k3DBackupStatus	
Write-Host $BackupFolder2K3DInfo
You were calling the function, right?

Also, since Test-Path already returns a boolean, you could omit the -ne $False, so, just write: if (Test-Path "C:\dell"). But, that is more of a style choice, just keep it readable for you and your colleagues.

Jelmylicious fucked around with this message at 16:44 on Dec 21, 2011

Wicaeed
Feb 8, 2005
Okay, thanks for that clarification. The reason the vbscript is run like it is, is because that the results are later send via email, and from what you said regarding functions, you can't actually store the results as a variable that exists outside the function?

I was under the impression that Functions functioned (:v:) somewhat like subroutes in vbscript. Is that not the case?

If I DID want to store the results to call back on, but keep it inside the function, is that even possible?

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!

Wicaeed posted:

Okay, thanks for that clarification. The reason the vbscript is run like it is, is because that the results are later send via email, and from what you said regarding functions, you can't actually store the results as a variable that exists outside the function?

I was under the impression that Functions functioned (:v:) somewhat like subroutes in vbscript. Is that not the case?

If I DID want to store the results to call back on, but keep it inside the function, is that even possible?

That is a fourth possibility, make it a global variable, by starting your variable with $global:

code:
Function get2k3DBackupStatus
{
If ((Test-Path "C:\dell") -ne $false)
{$global:BackupFolder2K3DInfo = "Drive backed up on $CurrentDate"}
Else
{$global:BackupFolder2K3DInfo = "Drive backup does not exists"}
}	
Write-Host $BackupFolder2K3DInfo

Moey
Oct 22, 2010

I LIKE TO MOVE IT

adaz posted:

You were very close -- just need the basename (which omits the file extension) and to put the piepline input in quotes so powershell expands it before attempting to pass it to the parameter - which results in a null being passed and the new-item trying to create the root directory over again

code:
gci | new-Item -name "$_.basename" -itemtype "directory"

Didn't know that name would grab the extension as well. I am still having a problem as $_ isn't pulling files from that directory. This script will just create a folder called .basement, then error out for the rest of the items.

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!

Moey posted:

Didn't know that name would grab the extension as well. I am still having a problem as $_ isn't pulling files from that directory. This script will just create a folder called .basement, then error out for the rest of the items.

Are you running it from the folder? Otherwise use GCI C:\PS (don't forget to add it to the beginning of your new foldername to).

Wicaeed
Feb 8, 2005
Sweet. HOWEVER, I have to be setting my function incorrectly, or something. When I call my function it returns a blank result, but if I comment out the function part of it so I'm left with:

code:
#Powershell function to check backup status Path D:\WindowsImageBackup on D: drive
#Function get2k3DBackupStatus
	#{
	If ((Test-Path "C:\dell") -ne $false)
		{$global:BackupFolder2K3Dinfo = "Drive backed up on $CurrentDate"}
	Else
		{$global:BackupFolder2K3Dinfo = "Drive backup does not exist"}
	#}
Write-Host $BackupFolder2K3DInfo
Which returns: Confluence D drive backed up on 12/21/2011 7:54:00 AM

Any ideas?

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!

Wicaeed posted:

Sweet. HOWEVER, I have to be setting my function incorrectly, or something. Any ideas?

You are not calling you function:

code:
Function Example 
    {
    write-host "Function has been run"
    }
will do nothing, while
code:
Function Example 
    {
    write-host "Function has been run"
    }

Example
will print Function has been run. Powershell only runs a main code block, and ignores functions until called in that block. The functions have to be in the beginning, because it does read it sequentually. So it won't know a function defined afterwards.

Wicaeed
Feb 8, 2005
:doh:

Yeah, now it works. Thanks a bunch :)

adaz
Mar 7, 2009

Moey posted:

Didn't know that name would grab the extension as well. I am still having a problem as $_ isn't pulling files from that directory. This script will just create a folder called .basement, then error out for the rest of the items.

Yeah sorry it was early in the morning, going to blame that instead of me just being dumb. Anyway what seems to be happening for some reason is the pipeline object is null (or at least New-Item thinks it is null). I don't know why this is the case and it's kind of pissing me off. Looking into it.

$blah is a get-childitem of the current directory.

Correct:


Being Stupid:

adaz fucked around with this message at 18:20 on Dec 21, 2011

greth
Nov 12, 2008

Does it trouble your mind the way you trouble mine?
Looks like you're forgetting your foreach:
code:
gci | %{new-item -name $_.basename.ToString() -type directory -path .}

    Directory: C:\Users\DURP\tmp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        12/21/2011  12:50 PM            a
d----        12/21/2011  12:50 PM            b
d----        12/21/2011  12:50 PM            c

That blasted little % is the key here.

adaz
Mar 7, 2009

e: Like a million edits but no you shouldn't need the foreach there because new-item should accept pipeline input on its parameters (and does according to the documentation)

adaz fucked around with this message at 19:07 on Dec 21, 2011

adaz
Mar 7, 2009

Trace-Command is our friend. It's because New-Item uses PropertyBinding (http://technet.microsoft.com/en-us/library/dd347566.aspx) on its parameters instead of value binding.

Basically with property binding if you're piping an object to that cmd-let it's going to bind its parameters to the same property name on the object that's being passed. So if it takes name as one of its parameters it's going to bind to name on the object no matter what you tell it to do. If the object doesn't have that property it'll actually error out on you (see the full explanation in get-help about_pipeline).

So in this case what we're seeing is New-Item is taking the name of the file instead of the basename and erroring out because the file exists, obviously. See here the trace (you can do this yourself by doing:

code:
Trace-Command -name parameterbinding -expression {gci | new-item -name $_.basename -type directory} -pshost


The reason Greth's solution worked is because we're inserting for-each cmd-let into the equation and New-Item no longer thinks it's in the pipeline and doesn't try to bind its parameters to the object being passed automatically.

adaz fucked around with this message at 20:36 on Dec 21, 2011

Moey
Oct 22, 2010

I LIKE TO MOVE IT
Thanks guys, that worked perfect. Now I am stuck on moving the files into the newly created folders.

I am trying to do something like this, but it is clearly wrong.

code:
gci | %{move-item $_ .\$_.basename.tostring()}
From the little knowledge I have, my logic is that would loop through the directory, grab the files, and then move them to the directory which name matches their file name. I am getting an error after the .tostring(

Edit

If I add in the full path, it will just renames the files without moving them. Both the lines below dont move anything, but rename both the files and the folders (appends a .basename to the end). Don't know why powershell is so difficult for me to grasp.

code:
gci | %{move-item $_ c:\ps\$_.basename}
gci | %{move-item $_ c:\ps\$_.basename\}

Moey fucked around with this message at 21:29 on Dec 21, 2011

Rabid Snake
Aug 6, 2004



This is another odd question. Is it possible to use Powershell to remove al sentences flagged by the Microsoft Word Grammar Checker? Or delete all sentences not flagged by Word's grammar checker? I'm checking the Word Object Model (http://msdn.microsoft.com/en-us/library/bb225019(v=office.12).aspx) but can't really find anything that returns if a sentence is flagged by Word's grammar checker.

edit:

Found something real interesting!

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.range.gotonext.aspx

or more specifically
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdgotoitem.aspx


Apparently the Range Interface provides a GoToNext method which contains a way to move the pointer to a grammar area. Time to mess with Powershell!

edit edit:

Found a way to find a grammatical incorrect sentence and I can delete it. The problem now is looping through the document. I can't seem to find a boolean value in the Office interop that returns true or false if the document still contains grammar errors...

Here's what I got so far
code:
cd c:\testruns\
$docPath = "" + $(Get-Location) + "\Grammar\document.docx"
$Word = New-Object -ComObject Word.Application
$Word.Visible = $True
$doc = $Word.documents.open($docPath)
$docSelection = $Word.selection

#WdGoToItem
$wdGoToSpellingError = 13
$wdGoToGrammaticalError = 14

#WdGoToDirection
$wdGoToFirst = 1
$wdGoToLast = -1
$wdGoToNext = 2
while (!$AnymoreGrammar) {
    [void]$docSelection.GoTo($wdGoToGrammaticalError, $wdGoToNext).delete()
}

I need a valid boolean test in the while loop...

Rabid Snake fucked around with this message at 23:27 on Dec 21, 2011

adaz
Mar 7, 2009

You are formatting the destination part wrong at first glance. Should be something like this

code:

gci | %{move-item -path $_.name -destination "$($_.directoryname)\$($_.basename)\$($_.name)" }

Moey
Oct 22, 2010

I LIKE TO MOVE IT

adaz posted:

You are formatting the destination part wrong at first glance. Should be something like this

code:

gci | %{move-item -path $_.name -destination "$($_.directoryname)\$($_.basename)\$($_.name)" }

Makes sense and works! It spits out errors but still moves the files. I think it is trying to move the folders as well. If I run it on a directory with 1 file, I get 1 error. 3 files, 3 errors. Don't think it matters, not sure though.

adaz
Mar 7, 2009

Yep it's the directories. If you wanted to filter them out instead of getting a bunch of random errors you can pipe Get-ChildItem to Where-Object {$_.psISContainer -eq $false} before you pass it to your for-each loop.


Rabid Snake not sure I have to look at the office interop stuff for a word project i'm working on I'll see if I spot anything there tomorrow.

adaz fucked around with this message at 04:30 on Dec 22, 2011

Wicaeed
Feb 8, 2005
So what's the proper way to go about getting folder paths with spaces to parse properly in powershell?

Example:

code:
$strCBackup = "wbadmin start backup -quiet -backuptarget:d: -include:""c:\Program Files\Atlassian"",c:\confluencedata\"
Returns the following:
code:
PS S:\Confluence_Backup> $strCBackup
wbadmin start backup -quiet -backuptarget:d: -include:"c:\Program Files\Atlassian",c:\confluencedata\
However when I try to use it in a function or use it with Invoke-Expression , wbadmin doesn't recognize the C:\Program Data\ as a valid path.

code:
ERROR - Command syntax incorrect. Error: Files\Atlassian c:\confluencedata". See the command 
syntax below
What's the correct way to get it to recognize that the folder exists with its spaces?

adaz
Mar 7, 2009

Single quotes ' are for string-literals which powershell won't try and expand -- see http://technet.microsoft.com/en-us/library/ee692790.aspx

Wicaeed
Feb 8, 2005
:argh:

code:
$strCBackup = "wbadmin start backup -quiet -backuptarget:d: ""-include:C:\confluencedata,C:\Program Files\Atlassian"""
I wish I really understood what these quotes and poo poo to, cause that works fine

Moey
Oct 22, 2010

I LIKE TO MOVE IT
Woop woop. Ran it today on the production data and everything worked. Could not get the moving to ignore folders, so when it hit that line, I had 1350 errors scrolling across my PS window, but it all worked out.

code:
#Renames file to append crap user wants
gci |rename-item -newname {$_.basename + " - Cat Picture.tif"}

#Creates folders based on first 9 chars
gci | %{new-item -name $_.tostring().substring(0,9) -type directory -path .}

#moves files into folders based on file number
gci | %{move-item -path $_.name -destination "$($_.directoryname)\$($_.tostring().substring(0,9))\$($_.name)" }
Thanks again everyone!

Edit: Just out of curiosity sake, how would I add "where-object {$_.psiscontainer -eq false}" to my last line of code there? Everything I try breaks it.

Moey fucked around with this message at 17:26 on Dec 23, 2011

Rabid Snake
Aug 6, 2004



adaz posted:

Rabid Snake not sure I have to look at the office interop stuff for a word project i'm working on I'll see if I spot anything there tomorrow.

Found one! If someone in the near future, for some odd reason, has the same problem I had, I just used the GrammaticalErrors Property.

msdn.microsoft.com/en-us/library/aa213190(v=office.11).aspx

So my while loop looked like this
code:
$errorCount = $doc.GrammaticalErrors.Count
while ($errorCount -ne 0)

Wicaeed
Feb 8, 2005
Okay, so I have this stupidly large function I am using to verify my backup results.

A previous function moves/renames the Windowsimagebackup folder to a network share, then this function verifies that that path exists, and if so removes the backup folder from the local drive.

Heres the code:

code:
Function getfinalbackupstatus
{
		If ((Test-Path  "S:\Confluence_Backup\ConfluenceC_$sDate") -ne $false){
			{
				$BackupFolder2K3CTmp =  "C:\ConfluenceC_$sDate"
				$BackupFolder2K3CFinal = "S:\Confluence_Backup\ConfluenceC_$sDate"
				$GetSize2K3CTmp = "Get-ChildItem $Backupfolder2K3CTmp -recurse | Measure-Object -Property length -Sum"
				$GetSize2K3CFinal = "Get-ChildItem $BackupFolder2K3CFinal -recurse | Measure-Object -Property length -Sum"
			}
				If (($getsize2k3ctmp.sum -eq $GetSize2K3CFinal.sum) -ne $false){
					Remove-Item $BackupFolder2K3CTmp -recurse
					$global:Backup2k3Status = "Confluence Data Backup Successful"
				}
				Else{
					$global:Backup2k3Status = "Confuence Data Backup Failed"
				}
		}		
		If ((Test-Path "S:\Confluence_Backup\ConfluenceD_$sDate") -ne $false){	
			{
				$BackupFolder2K3DTmp = "D:\$ConfluenceD_$sDate"
				$BackupFolder2k3DFinal = "S:\Confluence_Backup\ConfluenceD_$sDate"
				$GetSize2K3DTmp = "Get-ChildItem $Backupfolder2K3DTmp -recurse | Measure-Object -Property length -Sum"
				$GetSize2K3DFinal = "Get-ChildItem $Backupfolder2k3Dfinal -recurse | Measure-Object -Property length -Sum"
			}
				If (($GetSize2K3DTmp.sum -eq $GetSize2K3DFinal.sum) -eq $false){
						If ($Backup2k3Status -eq "Confluence Data Backup Successful"){
							Remove-Item $BackupFolder2K3DTmp -Recurse
							$global:Backup2k3Status = "Confluence Data Backup Successful"
						}
						Else{
							$global:Backup2k3Status = "Confuence Data Backup Failed"
						}
					}
				Else{
					$global:Backup2K3Status = "Confluence Data Backup FAILED"
				}
		}
		Else{
			$global:Backup2K3Status = "Confluence Data Backup FAILED"
		}
}
I realize that it's hideously long, however to me it's fairly straight forward. BUT, when it comes time to run, the second if statement contained in the codeblock always fails.

What's the right way to debug a function, if you have to load the function into memory before you can even run it later with the function name?

adaz
Mar 7, 2009

Trace-Command (http://technet.microsoft.com/en-us/library/dd315284.aspx) is what you'd want to use it is basically the powershell debugger.

By "second statement in the codeblock fails" do you mean

code:
If ((Test-Path "S:\Confluence_Backup\ConfluenceD_$sDate") -ne $false){
Always evaluates to false? What worries me is you're using $sDate inside the function but I don't see it defined anywhere in scope, so it'd have to be defined as a script or global variable somewhere else.

Powdered Toast Man
Jan 25, 2005

TOAST-A-RIFIC!!!
I'm new to Powershell, so please forgive me if this is a stupid question.

At my new job our AD forest is 2003 native. However, some of our DCs are running on 2008 R2. Is it at all possible to do AD administrative tasks (e.g. user creation/modification) with Powershell in our forest, or does it need to be 2008 native first?

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

Moey posted:


Thanks again everyone!

Edit: Just out of curiosity sake, how would I add "where-object {$_.psiscontainer -eq false}" to my last line of code there? Everything I try breaks it.

code:
#Renames file to append crap user wants
gci | where-object {$_.psiscontainer -eq $false} | rename-item -newname {$_.basename + " - Cat Picture.tif"}

#Creates folders based on first 9 chars
gci | where-object {$_.psiscontainer -eq $false} | %{new-item -name $_.tostring().substring(0,9) -type directory -path .}

#moves files into folders based on file number
gci | where-object {$_.psiscontainer -eq $false} | %{move-item -path $_.name -destination "$($_.directoryname)\$($_.tostring().substring(0,9))\$($_.name)" }
Note that it's $false not false

adaz
Mar 7, 2009

Powdered Toast Man posted:

I'm new to Powershell, so please forgive me if this is a stupid question.

At my new job our AD forest is 2003 native. However, some of our DCs are running on 2008 R2. Is it at all possible to do AD administrative tasks (e.g. user creation/modification) with Powershell in our forest, or does it need to be 2008 native first?

I believe only 1 DC has to be running 2008 R2 for it to work right. You also can just use the raw .NET classes (system.directoryservices) instead of the Active-Directory Modules. Which isn't that big of a deal, they are pretty easy to use and the classes/examples are well documented.

I believe quest software or someone like that makes a AD module that is just a frontend for the .NET system.directory classes for free that works without 2008 R2. I haven't used it myself.

Powdered Toast Man
Jan 25, 2005

TOAST-A-RIFIC!!!

adaz posted:

I believe only 1 DC has to be running 2008 R2 for it to work right. You also can just use the raw .NET classes (system.directoryservices) instead of the Active-Directory Modules. Which isn't that big of a deal, they are pretty easy to use and the classes/examples are well documented.

I believe quest software or someone like that makes a AD module that is just a frontend for the .NET system.directory classes for free that works without 2008 R2. I haven't used it myself.

Ok...well, we're looking at creating some scripts to automate new user creation/old user disabling tasks in our organization because we have a lot of user turnover. I'm assuming that if those scripts were created using the .NET classes we would not need to update them when in the future we transition to a native 2008 domain?

Moey
Oct 22, 2010

I LIKE TO MOVE IT

Jethro posted:

Note that it's $false not false

Thanks for that. Outside the $ I was pretty close just by trial and error. I had the same thing, but I had the conditional statement (psiscontainer) after move-item.

adaz
Mar 7, 2009

Powdered Toast Man posted:

Ok...well, we're looking at creating some scripts to automate new user creation/old user disabling tasks in our organization because we have a lot of user turnover. I'm assuming that if those scripts were created using the .NET classes we would not need to update them when in the future we transition to a native 2008 domain?

Correct they would continue to work just fine. I actually recommend people get used to the .NET classes as you know they will always work and are independent of DC Version/Forest Level/Powershell Version/Powershell installed modules. If you need any help let us know.

Wicaeed
Feb 8, 2005
How would one discover if a file was created on the first weekday in a month?

I can find a bunch of examples from years ago that use vbscript, but none do 100% what I need

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

Wicaeed posted:

How would one discover if a file was created on the first weekday in a month?

I can find a bunch of examples from years ago that use vbscript, but none do 100% what I need

code:
$info = Get-Item butts.txt
if($info.CreationTime.DayOfWeek -ne "Saturday" -and $info.CreationTime.DayOfWeek -ne "Sunday" -and $info.CreationTime.Day -lt 4)
{
    # it was the first weekday of the month
}
First we need to check if the creation time is not on a weekend, then make sure it's prior to the fourth day of the month (for the case where first day of the month is a Friday). Every other case should be covered but I haven't actually tested it.

Mario
Oct 29, 2006
It's-a-me!

i barely GNU her! posted:

code:
$info = Get-Item butts.txt
if($info.CreationTime.DayOfWeek -ne "Saturday" -and $info.CreationTime.DayOfWeek -ne "Sunday" -and $info.CreationTime.Day -lt 4)
{
    # it was the first weekday of the month
}
First we need to check if the creation time is not on a weekend, then make sure it's prior to the fourth day of the month (for the case where first day of the month is a Friday). Every other case should be covered but I haven't actually tested it.
What if the 1st is on a Monday? This would match Tuesday and Wednesday as well.

How about (again, not tested):
code:
$info = Get-Item butts.txt
if(($info.CreationTime.DayOfWeek -ne "Saturday" -and $info.CreationTime.DayOfWeek -ne "Sunday" -and $info.CreationTime.Day -eq 1)
-or
($info.CreationTime.DayOfWeek -eq "Monday" -and $info.CreationTime.Day -lt 4))
{
    # it was the first weekday of the month
}

Adbot
ADBOT LOVES YOU

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

Mario posted:

What if the 1st is on a Monday? This would match Tuesday and Wednesday as well.

How about (again, not tested):
code:
$info = Get-Item butts.txt
if(($info.CreationTime.DayOfWeek -ne "Saturday" -and $info.CreationTime.DayOfWeek -ne "Sunday" -and $info.CreationTime.Day -eq 1)
-or
($info.CreationTime.DayOfWeek -eq "Monday" -and $info.CreationTime.Day -lt 4))
{
    # it was the first weekday of the month
}
Good catch, that sounds right.

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