Archive

Posts Tagged ‘Script’

Move a Computer Object to an OU

2010/11/01 Leave a comment

source: http://technet.microsoft.com/en-us/library/cc731094(WS.10).aspx

I want to move a computername to the good OU depending of the SERVERNAME
In my company we use the name convention MTL1ASTT01:

MTL is the city name
1 is the site number
A for application
STT custom info
01 number of the server
In our Active Directory we use:
DOMAIN.local > GLOBAL > %SITENAME% > SERVERS
So if want to get the SITENAME from the Servername i would use the code below:
echo. Getting Sitename from the Computer name…
Set SITENAME=%COMPUTERNAME%:0,4%
echo. SITENAME is %SITENAME%
dsmove “CN=%COMPUTERNAME%,OU=COMPUTERS,DC=DOMAIN,DC=LOCAL” -newparent OU=Servers,OU=%SITENAME%,OU=GLOBAL,DC=DOMAIN,DC=LOCAL

Powershell – How to use a config file (ini, conf,…) with a Powershell Script ? Is it possible ?

2010/10/30 Leave a comment

source: http://serverfault.com/questions/186030/how-to-use-a-config-file-ini-conf-with-a-powershell-script-is-it-possibl

Hi

Is it possible to use a configuration file with a PowerShell script ?

for example, the configuration file:

#links
link1=http://www.google.com
link2=http://www.apple.com
link3=http://www.microsoft.com

and then call this information in the PS1 script:

start-process iexplore.exe $Link1

thanks in advance for your help!!

Your answers put me on the good track and I found this

SETTINGS.TXT

#from http://tlingenf.spaces.live.com/blog/cns!B1B09F516B5BAEBF!213.entry
#
[General]
MySetting1=value

[Locations]
InputFile="C:\Users.txt"
OutputFile="C:\output.log"

[Other]
WaitForTime=20
VerboseLogging=True

POWERSHELL COMMAND

#from http://tlingenf.spaces.live.com/blog/cns!B1B09F516B5BAEBF!213.entry
#
Get-Content "C:\settings.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }

then

After executing the code snippet, a variable ($h) will contain the values in a HashTable.

Name                           Value
----                           -----
MySetting1                     value
VerboseLogging                 True
WaitForTime                    20
OutputFile                     "C:\output.log"
InputFile                      "C:\Users.txt"

*To get an item from the table use the command $h.Get_Item(“MySetting1”).*

Categories: Knowledge, Powershell Tags: , ,

Powershell – Finding Old Files

2010/10/30 Leave a comment

source:http://powershell.com/cs/blogs/tips/archive/2008/11/20/finding-old-files.aspx

Occasionally, you might want to find files that are older than a give number of days to delete or backup those. A simple filter can provide that functionality:
filter FileAge($days) { if ( ($_.CreationTime -le (Get-Date).AddDays($days * -1) )) { $_ } }

Pipe the result of a Dir into FileAge filter, and it will only let those files pass that are at least the specified number of days old. The following line finds all PowerShell Script files in your personal folder that are at least 10 days old:

Dir $home\*.ps1 | FileAge 10

You could easily delete or backup the resulting files like this:

Dir $home\*.ps1 | FileAge 10 | Del -WhatIf
Categories: Knowledge, Powershell Tags: ,

Powershell – List Compressed Files/Folders

2010/07/20 Leave a comment

Source: http://stackoverflow.com/questions/1500114/powershell-wmi-and-compressed-files-folders

Powershell, WMI and compressed files/folders

Question:

I need to generate a script that will help me in getting a list of compressed files/folders (not zip files, but Windows compressed files) on a range of Windows 2003 servers. I have a client pc connected to the target servers and have access on a administrator role basis. My thoughts was to create a Powershell script to handle this problem using WMI or something else? But I’m kind of lost on the possibilities in the WMI world. Any hints/tips are appreciated.

Cheers

Answer:
I’m not sure if you can do that with WMI, then again I’m no WMI guru. If you can use PowerShell 2.0 this is pretty simple using the new remoting feature e.g.
$computers = 'server1', 'server2', 'server3'
$compressed = Invoke-Command $computers {Get-ChildItem C:\ -r -force -ea 0 | 
                 Where {$_.Attributes -band [IO.FileAttributes]::Compressed}}

Note that each file and dir object stored in $compressed will have an additional property PSComputerName that identifies which computer the deserialized object came from.

Alternatively, if you don’t have PowerShell 2.0 you could access the servers via a share e.g.:

$sharePaths = '\\server1\C$', '\\server2\C$', '\\server3\C$'
Get-ChildItem $sharePaths  -r -force -ea 0 |
Where {$_.Attributes -band [IO.FileAttributes]::Compressed}
This approach is likely to be slow.

Categories: Knowledge Tags: ,

Powershell – Get-ClipBoard Set-Clipboard

2010/05/20 Leave a comment

Source:http://stackoverflow.com/questions/1567112/convert-keith-hills-powershell-get-clipboard-and-set-clipboard-to-a-psm1-script

function Get-ClipBoard {
    Add-Type -AssemblyName System.Windows.Forms
    $tb = New-Object System.Windows.Forms.TextBox
    $tb.Multiline = $true
    $tb.Paste()
    $tb.Text
}

function Set-ClipBoard($text) {
    Add-Type -AssemblyName System.Windows.Forms
    $tb = New-Object System.Windows.Forms.TextBox
    $tb.Multiline = $true
    $tb.Text = $text
    $tb.SelectAll()
    $tb.Copy()
}



 

Categories: Powershell Tags: , ,