Friday, February 8, 2013

PowerShell: Passing PS Variables to Batch Files

I couldn't find many easy ways to transfer variables from PowerShell to cmd.exe batch files so I came up with my own solution.  We delete the old temp variable holding file, use PowerShell to create the variable and write the output to a go-between batch file, call that go-between batch file from your original batch file, and use the transferred variable.  Let me know if you don't get it, have questions, or have better suggestions:

1. Delete the variable-holding batch file from the last time it was used:
If exist d:\computername.bat del d:\computername.bat >nul

2.  From your batch file, create your PowerShell variable (I wanted to pick up the computer name):

powershell "$ComputerName = ('HQ-' + (gwmi win32_systemenclosure).SMBIOSAssetTag);write ('set PC=' + $computername)|out-file d:\computername.bat -encoding ASCII"

Note: In your new computername.bat file you have:
Set PC=HQ-R0xxxx
Which is a valid DOS batch command to create a variable named PC.

3. By writing the results into a batch file (computername.bat), you can call the new batch file from your original one.  Also, by writing a batch command to set a DOS variable, you've now transferred your PowerShell variable to a batch file.  From your original script:
call d:\computername.bat

4.  Now you can use the %PC% variable in your DOS batch file without having to load PowerShell each time you want to pull up the variable.  Obviously you can create all your variables from one PowerShell instance and have the command write out all the Set commands.

Also note, most systems have a restrictive PowerShell script execution policy so writing PS one-liners makes them easier to run since you don't have to worry about the script policy.

Example Batch File:


@Echo Off
if exist d:\computername.bat del d:\computername.bat >nul
PowerShell "$ComputerName = ('HQ-' + (gwmi win32_systemenclosure).SMBIOSAssetTag);write ('set PC=' + $computername)|out-file d:\computername.bat -encoding ASCII"
call d:\computername.bat
:InstallImage
cls
Echo.
Echo                               MAIN IMAGING MENU (%PC%)
Echo.
...


Saturday, February 2, 2013

PowerShellASP: Combining PowerShell, ASP, and HTML using IIS 7.x


PowerShellASP websites have the ability to run PowerShell on remote computers, much the same way as using -computername and -credential.  The site can detect the visitors client and retrieve information about their computer in order for the restricted user to perform elevated functions like install IP-based printers.

Here is a sample .ps1x file portion which creates a web form, asks for a username and password which is able to connect via RWMI, detects the visitors IP address, which OS they're using, connects to it using remote WMI and displays the Windows version in the web browser.  This is just a sample of what system administrators can do via this great product.  I encourage those who wish to automate processes and give limited elevated functionality to their users to visit the PowerShell ASP website and leverage this tool.

<form method="POST" action="" enctype="multipart/ form-data">
Username:  <input type=text name="username" size="20"value="<%=$request['username']%>" />
Password: <input type=password name="password" size="20" value="<%=$request['password']%>" />
<input type=submit  value="Check OS Version" />
</form>
<%
if($request.HttpMethod -eq "POST") {
$remotehost = [net.IPAddress]::Parse($request.UserHostAddress)
$remoteIP = [System.Net.Dns]::GetHostAddresses($remotehost)| ? {$_.AddressFamily -eq "InterNetwork"}
$username = ("$remoteip" + "\" + $request['username'])
$securePassword = ConvertTo-SecureString $request['password'] -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PsCredential($username,$securePassword)
$session = new-PSSession -credential $Credentials -computer 192.168.1.145
$command = {Gwmi -Class Win32_OperatingSystem|convertto-html -fragment -property version}
$remote = Invoke-Command -session $session -scriptblock $command
write-host $remote}
%>

Facebook: Blocking Unwanted App Requests

Your Facebook friends may send you invites to try their game or application.  Some games reward players for sending requests after they've achieved a milestone, gained a new score, or finished for the day.  If they comply, then they'll be rewarded with virtual points, gold, loot, and/or other goodies.  You can filter these requests from your newsfeed while they can still fulfill their games requests and gain their virtual bonuses.   Here's how to hide these game/application requests:

1.  You receive an invitation to play a game.  Click on the game link:

2.  At the bottom right side of the application/game page, click on "View in App Center"


3. At the bottom right portion of the App page, click on Block:


 4.  Click Confirm to block the App/Game: (there will be a confirmation page after your click)


5.  If you wish to view all of the applications/games you have blocked and wish to manage that list, find the gear icon at the top right portion of your Facebook page and click on Account Settings:


6.  Locate Blocking on the left side and click on it: 


7.  Towards the bottom of the Block page, you'll find all the blocked applications.  You can add more by typing in the name of the program and pressing the Enter key.  You can also choose to unblock programs by clicking on Unblock next to the application name.




Friday, February 1, 2013

Facebook: Hiding Suggested Pages

Fine tuning your Facebook news feed is a constant task.  To help bypass uninteresting items, simply perform the following:

1.  Find the down arrow located at the top right portion of the feed item to hide.


2. Select "Hide" or "Report Story or Spam" as you see fit.


3. You'll see confirmation that the story is hidden.



Thursday, January 31, 2013

PowerShell: Removing First 3 Characters from Filenames of Parent and Subfolders


I found several examples of renaming files but this one is simple and easy to change.

gci -include *.txt -recurse|ren -newname {($_.name).substring(3)}

Note: no carriage return, this is a single command
Note: Doesn't like spaces in the filenames

Run this command from the parent folder and it'll rename all the file types in the folder and all subfolders.  The only limitation I found is special characters in the folder or filename.