I wanted to make it look cool:
gci $env:SystemDrive\ -r "iexplore.exe" -af -ea 0 | % FullName
How to make it shorter with same functionality?
IT Esteet
News, code and more.
Thursday, July 23, 2015
Thursday, May 29, 2014
Change server for installed printers
While changing our printserver we needed a way to update configuration on all machines in the domain.
First, the printers were migrated from old server to new with exactly the same configuration.
To update workstations, Configuration Baseline was created in SCCM.
Discovery script (returning something else than 0 = not compliant):
Get-WmiObject -Class Win32_Printer | where { $_.SystemName -like "*OLDSERVER*" } | Measure-Object | select -ExpandProperty Count
Remediation script (returning $true will mean failure in process):
$fail = $false
Get-WmiObject -Class Win32_Printer | where { $_.SystemName -like "*OLDSERVER*" } | select Name, Default | foreach {
$newname = $($($_.name) -replace "OLDSERVER", "NEWSERVER")
write-host "Replacing $($_.name) with $newname"
#ADD NEW PRINTER
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection($newname)
if ($($_.default)) {
write-host "Above is default printer"
$default = $newname
}
#WAIT AND CHECK FOR SUCCESS
write-host "Waiting 30 seconds to check for success"
Start-Sleep 30
if ( $(Get-WmiObject -Class Win32_Printer | where { $_.Name -eq $newname } | Measure-Object | select -ExpandProperty Count) -gt 0 ) {
write-host "SUCCESS, removing old"
(New-Object -ComObject WScript.Network).RemovePrinterConnection($($_.name))
write-host "Setting printer as default"
if ($default) {
(New-Object -ComObject WScript.Network).SetDefaultPrinter($default)
$default = $null
}
} else {
write-host "FAIL, keeping old"
$fail = $true
}
}
write-host "Next line is True if errors, False if no errors:"
return $fail
The scripts were made quite fast and are not optimized, it is not needed as this is only a temporary configuration baseline. After old printserver is decommissioned, this baseline will be disabled.
Could this be done in a single line of code? :)
Labels:
baseline,
configuration,
gpo,
powershell,
printers,
printserver,
sccm,
server,
windows
Thursday, January 2, 2014
Get AD user groups with managers
Because we have implemented groups with managers in Active Directory, there is a need to take reports on user group memberships and also the owners of the groups the user belongs to.
I had a really long script for a while but I've got a bit better and pipe things shorter (should be a trademark), so here is my new attempt on this topic:
10 12 lines (gave a little edit for better reports). The original script had some comments and extra stuff in it, but it wont be missed.
If anyone has good hints on creating PSObjects with many properties in one line, let me know in comments ;)
I had a really long script for a while but I've got a bit better and pipe things shorter (should be a trademark), so here is my new attempt on this topic:
Instead of 68 lines I now get things done in
- $list = @()
- Get-ADUser -Filter "Name -like ""*""" -SearchBase "OU=Users,DC=contoso,DC=eu" -Properties name | % {
- $name = $_.name
- $_ | Get-ADPrincipalGroupMembership | % {
- $line = New-Object PSObject
- $line | Add-Member -MemberType NoteProperty -Name "Name" -Value $name
- $line | Add-Member -MemberType NoteProperty -Name "Group" -Value $($_.name)
- $line | Add-Member -MemberType NoteProperty -Name "Manager" -Value $($_ | Get-ADGroup -Properties managedBy | Select -ExpandProperty managedBy | Get-ADObject | Select -ExpandProperty Name)
- $list = $list + $line
- }
- }
- $list | export-csv "c:\temp\usergroups.csv" -Encoding Unicode
If anyone has good hints on creating PSObjects with many properties in one line, let me know in comments ;)
Labels:
active directory,
contoso,
groups,
pipe,
powershell,
psobject,
scripting,
user,
windows
Monday, December 16, 2013
Pass arguments to powershell in batch file
Sometimes you want to run Powershell script without .ps1 files (directly from batch file).
My last post was about that but now I'll show you how to pass arguments to your scriptblock.
It is relatively easy but not easily found from internet.
Here goes:
powershell -Command "& { Write-host $args[0] }" "ARGUMENTS_HERE"
The output should be "ARGUMENTS_HERE".
Real life example:
I needed this for simple script that is fired from another batch file with arguments of files, that need to be scanned for lines. If lines are found, e-mail is sent.
powershell -Command "& { Get-Childitem -Path $args[0] -Filter $args[1] | foreach { $len = get-content $_.FullName | measure-object -char; if ($len.Characters -gt 0) { Send-MailMessage -SmtpServer 'MAILSERVER HERE' -From 'SENDER HERE' -To 'RECEIVER HERE' -Subject $( 'Errors in log: ' + $($_.Name) ) } } }" "PATH HERE" "FILTER HERE"
It has been long since I posted anything here, I should make a better use for this blog as I search for my own code snippets pretty often :)
Merry Christmas!
Tuesday, December 18, 2012
Run powershell in windows batch file
I needed to run a powershell script in batch file so it would be executed as one line of commands, without requiring to run .ps1 file directly.
The task is fairly simple, I've done this before. But this time I ran into another problem, because the need of using " signs inside " signs that were actually around -command argument.
This leaded me to a problem that I couldn't use escape chars for " because it would escape it from script altogether.
I couldn't use ' signs either, because that would turn variable names into direct strings.
Here is what I had at start (works within powershell):
$date = Get-Content '.\card_dates.txt' | Select-Object -last 1;
Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) {
Get-Content $_.FullName | Foreach-Object {
$_ -replace '#PARAM_1', $date
} | Set-Content "$($_.FullName).parsed"
}
Here is the modification that gave me the error (if running from batch):
powershell -Command "& { $date = Get-Content '.\card_dates.txt' | Select-Object -last 1; Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) { Get-Content $_.FullName | Foreach-Object {$_ -replace '#PARAM_1', $date} | Set-Content "$($_.FullName).parsed" } } "
This is the error:
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:222
+ ... | Set-Content $($_.FullName).parsed } }
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-Content], ParameterBinding
ValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
icrosoft.PowerShell.Commands.SetContentCommand
So what did I change to make it work?
The solution was actually really simple.
Instead of using:
"$($_.FullName).parsed"
I had to use:
$($($_.FullName) + '.parsed')
And here is the solution that works directly from commandline
powershell -Command "& { $date = Get-Content '.\card_dates.txt' | Select-Object -last 1; Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) { Get-Content $_.FullName | Foreach-Object {$_ -replace '#PARAM_1', $date} | Set-Content $($($_.FullName) + '.parsed') } } "
What this script does?
The task is fairly simple, I've done this before. But this time I ran into another problem, because the need of using " signs inside " signs that were actually around -command argument.
This leaded me to a problem that I couldn't use escape chars for " because it would escape it from script altogether.
I couldn't use ' signs either, because that would turn variable names into direct strings.
Here is what I had at start (works within powershell):
$date = Get-Content '.\card_dates.txt' | Select-Object -last 1;
Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) {
Get-Content $_.FullName | Foreach-Object {
$_ -replace '#PARAM_1', $date
} | Set-Content "$($_.FullName).parsed"
}
Here is the modification that gave me the error (if running from batch):
powershell -Command "& { $date = Get-Content '.\card_dates.txt' | Select-Object -last 1; Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) { Get-Content $_.FullName | Foreach-Object {$_ -replace '#PARAM_1', $date} | Set-Content "$($_.FullName).parsed" } } "
This is the error:
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:222
+ ... | Set-Content $($_.FullName).parsed } }
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-Content], ParameterBinding
ValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
icrosoft.PowerShell.Commands.SetContentCommand
So what did I change to make it work?
The solution was actually really simple.
Instead of using:
"$($_.FullName).parsed"
I had to use:
$($($_.FullName) + '.parsed')
And here is the solution that works directly from commandline
powershell -Command "& { $date = Get-Content '.\card_dates.txt' | Select-Object -last 1; Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) { Get-Content $_.FullName | Foreach-Object {$_ -replace '#PARAM_1', $date} | Set-Content $($($_.FullName) + '.parsed') } } "
What this script does?
- Read last line from textfile (it is populated there by other scripts)
- Read all SQL files inside SQL folder
- Withing every file, change #PARAM_1 to be the last line from textfile
- Save the file with .parsed extension
Basically we have templates for SQL sentences and historically all the jobs run as BATCH files.
I'm better with Powershell so I wanted to do it in powershell but at the same time keep the old BATCH-es.
Hopefully this information is a bit helpful for you, I spent quite some time to figure out the simple solution.
I'm better with Powershell so I wanted to do it in powershell but at the same time keep the old BATCH-es.
Hopefully this information is a bit helpful for you, I spent quite some time to figure out the simple solution.
Thursday, November 8, 2012
Surface Pro details leaked
Although the RT version of Microsoft’s first tablet ever is already experiencing record sales, there’s no doubt that most people are actually expecting the Surface Pro.
And the reason is as simple as it could be: it will be able to run legacy Windows programs and won’t be limited to RT apps like the currently available Surface.
Microsoft hasn’t provided too much info on the Surface Pro, but German retailer Notebook.de has recently unveiled pricing for the device and even kicked off a pre-order campaign for local buyers.
The Surface Pro will come equipped with the actual Windows 8 operating system and will pack more advanced hardware as compared to the RT. Sources close to the matter suggest that it’ll feature a larger battery, a higher resolution display and more storage space, most likely up to 128 GB.
The German retailer lists two different Surface Pro versions, one with 128 GB and the other one with 64 GB of storage space.
Hardware information is still limited, but the 128 GB model is priced at $1165 (€909), while the 64 GB version is a bit cheaper – $1035 (€809). Since it’s a European Union state, Germany also has a Value Added Tax (VAT), so the device is going to be cheaper in the United States.
US buyers will be able to get the Surface Pro with 128 GB of storage space for $975 (€763) and the 64 GB flavor for $870 (€679).
While the Surface Pro is going to be a bit heavier than the Surface RT, it’ll provide a full Windows 8 experience. The device will be delivered with a stylus and will boast 4 GB of RAM to better handle the operating system and the installed apps. An Intel Core i5 processor is very likely to be installed on all Surface Pro versions.
Here are the technical specs provided by Notebook.de, but we cannot guarantee that all of these are indeed accurate. Microsoft is yet to issue a comment on the matter, but we’ve contacted the Redmondians and we’ll get back to you as soon as we receive an answer. The technical specification list provided by Microsoft is available here (PDF viewer needed).
- 10.6-inch full HD ClearType display
- 1920x1080 resolution
- Intel Core i5 processor
- 4 GB of RAM
- 128 GB / 64 GB of storage space
- Intel HD 4000 video card
- 802.11 a/b/g/n wireless card
- Bluetooth 4.0
- HD camera
- Vapor Magnesium case
- Total weight – 900g
Anticipating :)
Sunday, October 28, 2012
Surface tablet keynote
If you missed the keynote then now you have the chance to check it out. I am personally quite impressed about the touch cover, which I didn't like from the pictures. The overall quality of the device seems superb, I love how thorough Microsoft has been when developing this product.
But does the RT version come with desktop mode also or are the ones in the end of demo pro versions?
But does the RT version come with desktop mode also or are the ones in the end of demo pro versions?
I thought it will be unavailable on RT.
My next PC will be surface :)
Subscribe to:
Comments (Atom)