Tuesday, March 25, 2014

Access And Powershell: How to populate a table

Here's the code:

function GetValue
{
    param
    (
    [string]$Name,
    [object]$obj
    )

    [string]$PName = $Name + " = "
    [string]$tempstr = $obj.GetObjectText_(0)

    $pos = $tempstr.IndexOf($PName)
    if ($pos -gt 0)
    {
        $pos = $pos + $PName.Length
        $tempstr = $tempstr.SubString($pos, ($tempstr.Length - $pos))
        $pos = $tempstr.IndexOf(";")
        $tempstr = $tempstr.SubString(0, $pos)
        $tempstr = $tempstr.Replace('"', "")
        $tempstr = $tempstr.Replace("}", "")
        $tempstr = $tempstr.Replace("{", "")
        $tempstr = $tempstr.Trim()
        if($tempstr.Length -gt 14)
        {
            if($obj.Properties_.Item($Name).CIMType -eq 101)
            {
                [System.String]$tstr = $tempstr.SubString(4, 2)
                $tstr = $tstr + "/"
                $tstr = $tstr + $tempstr.SubString(6, 2)
                $tstr = $tstr + "/"
                $tstr = $tstr + $tempstr.SubString(0, 4)
                $tstr = $tstr + " "
                $tstr = $tstr + $tempstr.SubString(8, 2)
                $tstr = $tstr + ":"
                $tstr = $tstr + $tempstr.SubString(10, 2)
                $tstr = $tstr + ":"
                $tstr = $tstr + $tempstr.SubString(12, 2)
                $tempstr = $tstr
            }
        }
        return $tempstr
    }
    else
    {
        return ""
    }
}

param
(
    Manditory=$True
    $Databasename
)

    $l = new-object -comobject Wbemscripting.SWbemLocator
    $svc = $l.ConnectServer(".", "root\cimv2")
    $svc.Security_.AuthenticationLevel=6
    $svc.Security_.ImpersonationLevel=3
    $ob = $svc.Get("Win32_Process")

    $oaccess = new-object -comobject Access.Application
    $oaccess.CreateCurrentDatabase($DatabaseName, 9)

    $db = $oAccess.CurrentDB()
    $tbldef = $db.CreateTableDef("Processes_Properties")

    foreach($prop in $ob.Properties_)
    {
        $fld = $tbldef.CreateField($prop.Name, 12)
        $fld.AllowZeroLength = $true
        $tbldef.Fields.Append($fld)
    }
    $db.TableDefs.Append($tbldef)
   
    $objs = $ob.Instances_(0)

    $rs = $db.OpenRecordset("Select * From Processes_Properties", Exclusive:=False)
    foreach($obj in $objs)
    {
        $rs.AddNew()
        foreach($prop in $obj.Properties_)
        {
            $rs.Fields($prop.Name).Value = GetValue $prop.Name $obj
        }
        $rs.Update()
    }

Saturday, March 22, 2014

How to acquire a list of special folders

If you were wondering how to acquire a list of special folders, this is an easy way of doing it:

$ws = new-object -comobject wscript.shell
$rt = $ws.SpecialFolders

When you type $rt on the next line:

$rt
And press enter, Powershell will display a list of special folders like this:

C:\Users\Public\Desktop
C:\ProgramData\Microsoft\Windows\Start Menu
C:\ProgramData\Microsoft\Windows\Start Menu\Programs
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
C:\Users\Administrator\Desktop
C:\Users\Administrator\AppData\Roaming
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Templates
C:\Windows\Fonts
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Network Shortcuts
C:\Users\Administrator\Desktop
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\SendTo
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Recent
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Star
C:\Users\Administrator\Favorites
C:\Users\Administrator\Documents
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

Suppose you knew that the special folder for fonts would always be the 9th one -- it is zero based:








$rt.Item(8)
C:\Windows\Fonts

Friday, March 21, 2014

Building Shortcut Scripts Through Automation

Due to popular demand for information on how to create shortcut scripts, I'm wondering if anyone would be interested in a tool that would help you do this?

Please reply if you are interested.

Here's a screenshot of the tool:





Here's the results:


Wednesday, March 19, 2014

How To Run Control Panel Wizzards Using Powershell

Here's a link to a site that shows how to do these using VB6:

http://vbnet.mvps.org/index.html?code/system/controlpnl.htm

To accomplish the same in Powershell, simply copy the command line parameters:

rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,1

And then place quotes around it as shown below:

$ws = new-object -comobject wscript.shell
$ws.Run("rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,1")

Thursday, March 13, 2014

Powershell: How to create an Inputbox

Here's how you can create an InputBox in Powershell:

$Iret = [reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$Answer = [Microsoft.VisualBasic.Interaction]::InputBox("Please Enter a language", "Language Selection", "C#")

Powershell: Using Visual Basic's CreateObject

Here's how you can use CreateObject in Powershell:

$Iret = [reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$Iret = [Microsoft.VisualBasic.Interaction]::CreateObject("ADODB.Connection")

How To Create A msgbox in Powershell

One of the rather charming features of Powershell is its ability to enable you to use other existing .Net framework languages. I like it because it makes you feel like you aren't giving up on what you already know.

Such is the case with the Msgbox:

$Iret = [reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$Iret = [Microsoft.VisualBasic.Interaction]::MsgBox("This works just fine")

How To Create A Messagebox In Powershell

Here's how to create a Messagebox:

$Iret = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Iret = [System.Windows.Forms.MessageBox]::Show("Hello World")

There you go.