Solved

Drop File via Powershell

Posted on 2016-11-03
7
40 Views
Last Modified: 2016-11-03
Hi all,

oBdA was very helpful in proving the below code. I need to however expand it slightly and allow it to only run once.

I thought the best way to achieve this would be to create a file once it's ran. Then any future logins the script will check for that file and if present will not change the power scheme setting again. I would like the file to be called PowerSettingMod.txt and inside a line of text stating "The power setting was successfully applied on DATE"

If ((& powercfg.exe /GetActiveScheme) -match '(?<GUID>[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12})') {
	$currScheme = $Matches['GUID']
	"Current scheme is '$($currScheme)'" | Write-Host
	& powercfg.exe /SetDCValueIndex $currScheme '4f971e89-eebd-4455-a8de-9e59040e7347' '5ca83367-6e45-459f-a27b-476b1d01c936' 0
} Else {
	"Could not query the active scheme!" | Write-Error
}

Open in new window

0
Comment
Question by:ranski
  • 4
  • 3
7 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41873043
$FlagFile = "C:\Temp\PowerSettingMod.txt"
$FlagText = "The power setting was successfully applied on $(Get-Date)"
If (Test-Path -Path $FlagFile) {
	"Change has already been applied!" | Write-Warning
} Else {
	If ((& powercfg.exe /GetActiveScheme) -match '(?<GUID>[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12})') {
		$currScheme = $Matches['GUID']
		"Current scheme is '$($currScheme)'" | Write-Host
		& powercfg.exe /SetDCValueIndex $currScheme '4f971e89-eebd-4455-a8de-9e59040e7347' '5ca83367-6e45-459f-a27b-476b1d01c936' 0
		$FlagText | Set-Content -Path $FlagFile
	} Else {
		"Could not query the active scheme!" | Write-Error
	}
}

Open in new window

0
 

Author Comment

by:ranski
ID: 41873049
Top! thanks.

Any chance we can get the date in UK format?
The power setting was successfully applied on 11/03/2016 21:26:50
The power setting was successfully applied on 03/11/2016 21:26:50

I also noticed that it fails unless I create the Temp folder first
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41873069
I was assuming you already had a folder where you put stuff like this, but this will create the target folder if it doesn't exist yet.
For the time, you can use the en-UK or en-GB culture info, or just specify the format you want:
$FlagFile = "C:\Temp\PowerSettingMod.txt"
$TimeStamp = Get-Date -Format 'MM/dd/yyyy HH:mm:ss'
# $TimeStamp = Get-Date -Format ((New-Object System.Globalization.CultureInfo("en-GB")).DateTimeFormat.FullDateTimePattern)
$FlagText = "The power setting was successfully applied on $($TimeStamp)"
If (Test-Path -Path $FlagFile) {
	"Change has already been applied!" | Write-Warning
} Else {
	If ((& powercfg.exe /GetActiveScheme) -match '(?<GUID>[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12})') {
		$currScheme = $Matches['GUID']
		"Current scheme is '$($currScheme)'" | Write-Host
		& powercfg.exe /SetDCValueIndex $currScheme '4f971e89-eebd-4455-a8de-9e59040e7347' '5ca83367-6e45-459f-a27b-476b1d01c936' 0
		$FlagFolder = [IO.Path]::GetDirectoryName($FlagFile)
		If (-not (Test-Path -Path $FlagFolder) {
			New-Item -ItemType Directory -Path $FlagFolder | Out-Null
		}
		$FlagText | Set-Content -Path $FlagFile
	} Else {
		"Could not query the active scheme!" | Write-Error
	}
}

Open in new window

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:ranski
ID: 41873081
Thanks,

I get the below error

At C:\Untitled1.ps1:13 char:42
+         If (-not (Test-Path -Path $FlagFolder) {
+                                                ~
Unexpected token '{' in expression or statement.
At C:\Untitled1.ps1:13 char:42
+         If (-not (Test-Path -Path $FlagFolder) {
+                                                ~
Missing closing ')' after expression in 'If' statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
0
 
LVL 84

Assisted Solution

by:oBdA
oBdA earned 500 total points
ID: 41873086
Sorry; just add another closing round bracket after "$FlagFolder)", so that line 13 looks like this:
		If (-not (Test-Path -Path $FlagFolder)) {

Open in new window

0
 

Author Comment

by:ranski
ID: 41873098
perfect thanks.

The time still reports as US but not a bid deal really

The power setting was successfully applied on 11/03/2016 21:58:10
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41873104
Just swap the "dd" and "MM" (case sensitive!) in line 2 if you want the day first.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A brief introduction to what I consider to be the best editor for PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question