Friday, May 24, 2013

Force PowerShell scripts to run from C:\scripts

Here is a little tidbit I came up with because I like to be picky about how my scripts run.  It forces a script to relocate itself and run from the C:\Scripts folder.  I usually prefer to place the comments that describe a line on the end of that line.  In this case I had to go below the line.


$ErrorActionPreference = "SilentlyContinue"
$rand = New-Object System.Random

#--------------------[ Create and relocate to C:\scripts ]----------------------
if (!(Test-Path -pathtype Container "C:\Scripts")){
   new-item C:\Scripts -itemType Directory -Force  #--[ Create local scripts folder.
}

$TargetPath = "C:\Scripts"                              #--[ Where the script "should" live
$ScriptFullName = ($MyInvocation.MyCommand).Name        #--[ EX: script.ps1
$ScriptFullPath = ($MyInvocation.MyCommand).Path        #--[ EX: C:\Scripts\script.ps1
$ScriptHomeDir = split-path -parent $ScriptFullPath     #--[ EX: C:\Scripts, where the script resides
$ScriptWorkingDir = $pwd.path                           #--[ EX: C:\Scripts or C:\temp, where the script executes from
$ScriptShortName = [system.io.path]::GetFilenameWithoutExtension($ScriptFullPath)     #--[ EX: script (no extention)
$ErrorDetected = $False
$osv = [environment]::osversion.VersionString           #--[ Get Windows version, not required, just for convenience
$windir = [System.Environment]::ExpandEnvironmentVariables("%WINDIR%")     #--[ Get %windir% environment variable
#$windir = $env:windir                                  #--[ Alternate format
$ThisComputer1 = [System.Net.Dns]::GetHostName()        #--[ NOTE: No reason for both forms, just because.
$ThisComputer2 = $env:COMPUTERNAME
$eMailDomain = "gmail.com"                              #--[ Domain where emails will be sent
$ErrorDetected = $false

#--------------------[ Assure things run from C:\scripts ]----------------------
if (!($ScriptHomeDir -eq $TargetPath)){                 #--[ Paths are NOT same ]--
   if (!(Test-Path -path $TargetPath)){                 #--[ Does target path exist?  If not, create it ]--
      New-Item $TargetPath -type directory
   }
   if (!(Test-Path "$TargetPath\ThisScriptName")){      #--[ Is the script there?  If not, copy it there ]--
      Copy-Item $ScriptFullPath $TargetPath\$ScriptFullName
   }
   $WshShell = New-Object -comObject WScript.Shell      #--[ Place a shortcut on the desktop of current user to call the script
   $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\MyScript.lnk")
   $Shortcut.TargetPath = 'powershell.exe'
   $Shortcut.Arguments = "-WindowStyle Hidden –Noninteractive -NoLogo -Command `"$TargetPath\$ScriptFullName`""
   $Shortcut.Arguments =
   $Shortcut.Save()

   #iex (.{Join-Path $TargetPath \$ScriptFullName})
      #--[ re-invoke the script from the new location ]--
   #EXIT    
      #--[ Force termination ]--
}Else{        
      #--[ Paths ARE good, script exists, OK to execute ]--
   #if (!(Test-Path "$ScriptPath\ThisScriptName"))        
      #--[ an optional additional check ]--
}
 

Tuesday, May 14, 2013

Mommy my desktop icons keep disappearing...

Well it's been a while.  I've since gotten a great new job and am back on my feet.  Busy as all get-out but that's a good thing.

I thought I'd post this since even though it was easy to find I had never heard of it before and I expect others will be looking for info on it as well.


I've been investigating a bizarre issue with desktop icons that's been plaguing users here at the new job. Here is what I found as well as an associated fix.


This is the response to a question posed about desktop shortcuts mysteriously disappearing:
This is a well-known problem, which as it turns out is actually a kind of twisted "feature" of Windows 7. 

Basically, there's a script that Windows 7 runs that regularly checks your desktop shortcuts and if it finds more than 4 of them "broken" (i.e., pointing to something that's not available at the moment), it removes all the "broken" ones!

I guess Microsoft feels somehow responsible for helping you keep your desktop clean. Perhaps this is a Good Thing for the non-technical people in their commercials who think up things in the back of a taxi.
This is the script that governs this activity:
C:\Windows\Diagnostics\Scheduled\Maintenance\TS_BrokenShortcuts.ps1

If you look in it, not too far from the end is a statement that compares the length of the list of broken shortcuts to 4. It's the only occurrence of the numeral 4 in the entire script.  If you replace the 4 with a very large number, your problem should be solved.

There is also a Support Article and hot-fix from Microsoft that claims to fix the issue here: https://support.microsoft.com/kb/2642357
Here are scenarios that will trigger this:
  • You create five or more shortcuts on the desktop of a computer that is running Windows 7.
  • These shortcuts are pointed to an external location. For example, the shortcuts are pointed to network resources or to removable storage devices.
  • The computer is disconnected from the network where the network resources reside. Or, the removable devices are disconnected from the computer.
  • You run the System Maintenance troubleshooter on the computer. (this also runs on a schedule)
The third item means ANY disconnect. That means network congestion, server overload, IPSEC issues, firewall issues, issues with the local PC that cause it to pause (CPU over loaded or RAM overloaded).  If you see the dreaded red X on your mapped drive that would indicate a disconnect.

The easiest way to fix this problem is go to Control Panel; Action Center; Troubleshooting; click Change Settings on the left hand side; then turn Computer Maintenance off.

I found out that to edit the file (which is protected) you first need to take ownership of it (or the folder), then reset the permissions of the user/group you just gave ownership to to "full control".  Once that's done you can COPY the file and paste a new copy, then delete the original and rename the copy using the old name.  At this point you can edit it.  I set the offending count to 499 and (so far) so good.