Solved

Drop File via Powershell

Posted on 2016-11-03
7
35 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 83

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 83

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 83

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 83

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

Problems using Powershell and Active Directory?

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

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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