Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Drop File via Powershell

Posted on 2016-11-03
7
Medium Priority
?
46 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 85

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 85

Accepted Solution

by:
oBdA earned 2000 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
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 

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 85

Assisted Solution

by:oBdA
oBdA earned 2000 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 85

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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