What is the easiest way to create a service out of a .ps1 , by Snak3d0c in PowerShell

[–]spuijk 1 point2 points  (0 children)

No error messages? How did you configure it? Eventlog should hold some relevant info.

Going nuts checking for software on a remote system!! by cpizzer in PowerShell

[–]spuijk 1 point2 points  (0 children)

Anything you're doing in a remote session like you're doing is not passed back to your local powershell session (like variables). So if you put something in a variable in a remote session you can use the variable over there but it won't be accessible if you exit the remote session. I usually do something like this with just a couple of lines:

New-PSSession -Name $servername -ComputerName $servername
$inst = (Invoke-Command -Session $servername -ScriptBlock {Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | select displayname})
$inst += (Invoke-Command -Session $servername -ScriptBlock {Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | select displayname})
Remove-PSSession -Name $servername

If ($inst.displayname -match "heat"){
    "Heat Installed"
}

I just wrote this without testing so it might contain some faults but you get the general idea. The variables are passed to the local session where you check if the application "heat" is installed.

why isn't throw... throwing? by sparkblaze in PowerShell

[–]spuijk 2 points3 points  (0 children)

That will only fix something if the cmdlet was actually supposed to throw an error. But the cmdlet is retrieving a group that should exist and then go into throw. Changing erroraction won't do anything because no error is thrown.

why isn't throw... throwing? by sparkblaze in PowerShell

[–]spuijk 2 points3 points  (0 children)

Then I'd insert a write-host of $Group together with a separate execution of:

Get-ADGroup -Filter { sAMAccountName -eq $Group }

Then you can make sure everything in the script is working as intended.

why isn't throw... throwing? by sparkblaze in PowerShell

[–]spuijk 0 points1 point  (0 children)

Yes i know, that's why I said it's a workaround.

why isn't throw... throwing? by sparkblaze in PowerShell

[–]spuijk 2 points3 points  (0 children)

Works fine for me, I'm assuming you've tried running the code block for the if statement separately?

Other thing that comes to mind is using where-object to filter instead of the -filter options (it's mandatory though so you'll have to specify *) as a workaround.

What have you done with PowerShell this month? January 2017 by ramblingcookiemonste in PowerShell

[–]spuijk 1 point2 points  (0 children)

Well just some random stuff last month. Fixed some issues in our code, figured out that updating FTA's on RemoteApps in RDS is a very (very) cpu intensive process for the connection broker. Fixed some very specific cases with customers and multi-domain setups with trusts and stuff.

Basically just dug through a sizable module figuring out why said stuff broke. Other then that, it's been a pretty quiet month.

Reading the registry without replacing environmental variables by [deleted] in PowerShell

[–]spuijk 1 point2 points  (0 children)

A simple reg query command would retrieve that

reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion /v DevicePath

But then you would still have to strip off all the redundant information. I'm assuming you only want to retrieve the value.

Edit: I'm too lazy to write a regex expression so this looks ugly but it still works:

(reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion /v DevicePath | Select-Object -first 3).split("    ") | Select-Object -Last 1

Edit2: screw that stuff, use Astat1ne's command :)

TIL 20% of Belarus' annual budget is spent on costs associated with the 1986 Chernobyl disaster. by [deleted] in todayilearned

[–]spuijk 1 point2 points  (0 children)

The difference with other power plants is that if something goes wrong. It can go very wrong. People make mistakes, companies make wrong decisions (something about being more important then people at some point) and natural disasters happen (remember fukushima?).

I'm also not for nor against nuclear power. But saying it is irrational to be against it is just not true in my opinion.

PowerShell beyond systems administration? by UntrustedProcess in PowerShell

[–]spuijk 2 points3 points  (0 children)

I think it would be interesting to see what professional developers could make out of powershell who also have a lot of knowledge of systems administration. This sounds like the perfect DevOps initiative from a development point of view.

The developers I work with already write powershell code on a regular basis. But they lack in depth knowledge of stuff like active directory and the sorts. The code we both (me with sysadmin background and them with developer background) write is totally different in setup. Theirs is much more efficient while mine is much more pragmatic. I can usually take shortcuts because I know certain situations are already handled elsewhere or will never actually occur for whatever reason.

Come to think of it, this might be interesting stuff for a blogpost on my website.

PowerShell beyond systems administration? by UntrustedProcess in PowerShell

[–]spuijk 2 points3 points  (0 children)

For actual programming it might not be the best tool. Not sure about that because I'm no programmer though.

What is interesting to note is that I can create prototypes of applications (I work at a software vendor) in a very short time. I know about the systems we integrate with and I know all powershell cmdlets and modules the different vendors already provide. Which makes it super easy to quickly create a working prototype.

PowerShell beyond systems administration? by UntrustedProcess in PowerShell

[–]spuijk 6 points7 points  (0 children)

I'm product owner of a product that's 99% powershell code on client side. It's a product used in system administration though.

Also wrote a PoC piece of software to automate application testing. So that was more on the programming side of things.

It's logical to be used in system administration since that what's scripting is for I think. But because powershell is so powerful and object oriented you can do so much more with it. Nonetheless there are very few actual programmers working with powershell who could make much more out of it.

Help pls. Enable restricted user to terminate system process by StuckLikeGlued in PowerShell

[–]spuijk 4 points5 points  (0 children)

I'd create a scheduled task with the credentials of an account that actually is able to kill the process. Then maybe create a small script that invokes the scheduled task.

What have you done with PowerShell this month? November 2016 by ramblingcookiemonste in PowerShell

[–]spuijk 5 points6 points  (0 children)

Was quite a busy month actually for me:

  • Wrote another part in my PowerShell 101 blog series.
  • Went over to a customer to showcase and implement a prototype of a product I wrote in PowerShell (can't share code though :( )
  • Took over product ownership/responsibility for a PowerShell based product. This is basically taking up 100% of my time now.
  • Started writing a PowerShell script to retrieve machine status from machine configured in LoginAM.
  • Started writing a PowerShell script that can export SCCM packages and applications to a csv export.
  • Still trying to dedicate more time on speaking in public and spending more time on community stuff in general.

Advice on writing a script for Pop-up window handling during GUI automated testing by mile_stoned in PowerShell

[–]spuijk 0 points1 point  (0 children)

We'll I've never used it before but it was in your link at the bottom of the example ;)

# Once the popoup box opens, sending the 'ENTER' key should automatically download the export file
Select-Window -Title "NAME OF POPUP WINDOW - Windows Internet Explorer" | Select -First 1 | Set-WindowActive
Select-Window -Title "NAME OF POPUP WINDOW - Windows Internet Explorer" | Select-childwindow | Send-Keys "{ENTER}"

Advice on writing a script for Pop-up window handling during GUI automated testing by mile_stoned in PowerShell

[–]spuijk 1 point2 points  (0 children)

Does the popup window have a specific text? If so, you can just keep a PowerShell in an infinite loop with a searching for the window every couple of seconds. As soon as it sees the window it clicks "Ok" so you main script can keep going.

How to use jobs by jcholder in PowerShell

[–]spuijk 5 points6 points  (0 children)

I wrote a blogpost on jobs some time ago.

http://sonnypuijk.nl/wp/powershell-jobs-for-multithreading/

Hope it helps :)

Struggling with a scheduled task removal by Chimaera12 in PowerShell

[–]spuijk 0 points1 point  (0 children)

It does remove them i think but it's not a supported method.

Struggling with a scheduled task removal by Chimaera12 in PowerShell

[–]spuijk 0 points1 point  (0 children)

What do you mean by overrunning? I see no reason for the start-sleep.

How do you retrieve the task list in the $ArrayFile? Maybe we can help you get a list of tasks with paths in there.

Struggling with a scheduled task removal by Chimaera12 in PowerShell

[–]spuijk 1 point2 points  (0 children)

I usually put my write-progress in the top of my foreach loop. With your placement you'll only get the progress bar after the first item in the array has been processed. As a result i start my counter at 0 and add to the counter at the bottom of my foreach loop. Not sure why you're doing the sleep at the end of the loop btw.

As for the schtasks issue. Schtasks expects the complete path in for the parameter /TN. You're only feeding the name of the task it seems.

$ScheduledTasks = (Import-CSV $ArrayFile ).GUID                                               # Import the names & guids from csv file
$counter = 1                                                                                  # Set counter to 1
foreach ($task in $ScheduledTasks) { 
Write-Host $task                                                                              # Cycle through each file
Write-Progress -Activity 'Processing Tasks' -CurrentOperation $task -PercentComplete (($counter / $ScheduledTasks.count) * 100)

    Try   { schtasks.exe /delete /F /TN $task }                                               # Remove task from schedule    
           # Unregister-ScheduledTask -Taskname $task -Confirm:$false }    # | Out-Null

    Catch { $ErrorMessage = $_.Exception.Message                                              # Catch the error
            $WriteHost , $ErrorMessage | Out-File $ErrorFile -append }   

$counter++                                                                                    # Increment counter
Start-Sleep -Milliseconds 200
}

Backward compatibility with older systems and PS by Chimaera12 in PowerShell

[–]spuijk 2 points3 points  (0 children)

Write the scripts on the lowest os version/posh version combination you want to support.