Admin script sticky for PowerShell and/or Shell scripts

Status
Not open for further replies.

Godkills

Seniorius Lurkius
39
I'm sure I borrowed pieces of code from some other people, but these are a couple that I use regularly.


Script to get scheduled task domain accounts.
Code:
Import-module ActiveDirectory
$domain = "domainname*"

$servers = Get-ADComputer -Filter {operatingsystem -like "*server*"} -properties Name,DNSHostname | sort Name
$Output = @()

ForEach ($server in $servers) {
   # Ping the machine to see if it's on the network
   $results = Get-WMIObject -query "select StatusCode from Win32_PingStatus where Address = '$($server.name)'"
   $responds = $false  
   ForEach ($result in $results) {
      # If the machine responds break out of the result loop and indicate success
      if ($result.statuscode -eq 0) {
         $responds = $true
         break
      }
   }
         If ($responds) {
      # Gather info from the server because it responds
      $schtask = schtasks.exe /query /s $server.Name /V /FO CSV | ConvertFrom-Csv | Where { $_.TaskName -ne "TaskName" }
      $services = $schtask | where { $_."Run As User" -like "$domain"} 
      $Output += $Services
   } else {
      # Let the user know we couldn't connect to the server
      Write-Output "$($server.name) does not respond"
   }
}
$Output | export-csv c:\Scripts\task_accounts.csv

Script to get domain service accounts on servers.
Code:
Import-module ActiveDirectory
$domain = "domainname*"

$servers = Get-ADComputer -Filter {operatingsystem -like "*server*"} -properties Name,DNSHostname | sort Name
$Output = @()

ForEach ($server in $servers) {
   # Ping the machine to see if it's on the network
   $results = Get-WMIObject -query "select StatusCode from Win32_PingStatus where Address = '$($server.name)'"
   $responds = $false  
   ForEach ($result in $results) {
      # If the machine responds break out of the result loop and indicate success
      if ($result.statuscode -eq 0) {
         $responds = $true
         break
      }
   }
         If ($responds) {
      # Gather info from the server because it responds
      $Services = Get-WMIObject Win32_Service -ComputerName $($server.name) | Where-Object{$_.StartName -like $domain } | Select Name,State,StartName,SystemName
      $Output += $Services
   } else {
      # Let the user know we couldn't connect to the server
      Write-Output "$($server.name) does not respond"
   }
}
$Output | export-csv c:\Scripts\svc_accounts.csv
 

Cookie.Monster

Ars Scholae Palatinae
666
Yeah - these pop up every so often:
viewtopic.php?f=17&t=1122878
viewtopic.php?f=15&t=1225349
viewtopic.php?f=20&t=1179224

Not sure if a forum is the best place to try to consolidate scripts given how diverse the languages and goals for these scripts can be, but would be happy to contribute if one is set up. I post my favorites every so often in the 'what did you learn today?' thread, though it probably gets old for those not on the MS side of the house : )

Cheers!
 
This is to flip a file on a Windows server or CIFS share to read only. Had a need for it with using WORM on an IBRIX array that I didn't want to automatically commit right away.

Usage: .\<script file> <PATH> <Log file PATH>

Code:
    param (
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true
        )]
            [string] $Path,
            [string] $Results
    )
    process {
        $Files = @(GCI $Path -Recurse | Where-Object {!$_.isreadonly})
             Foreach($file in $files) {
                 Write-Host $File.name $File.isreadonly
                 Write-Output ('{0},{1},{2}'-f $Path,$File.name,$File.isreadonly) | Out-File $Results -Append
                 $File.isreadonly = $True
             }
    }
 

M. Jones

Ars Tribunus Angusticlavius
9,988
[url=http:/civis/viewtopic.php?p=26401959#p26401959:1huflyfi said:
Godkills[/url]":1huflyfi]I'm sure I borrowed pieces of code from some other people, but these are a couple that I use regularly.

It's useful to create comments with the original source of the script, and notes. This also makes it easier to find updated versions.
 
Status
Not open for further replies.