Solved

Drop File via Powershell

Posted on 2016-11-03
7
24 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now