Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Thursday, September 6, 2018

Validate AX DLL Versions on Multiple Machines

List the versions for all DLLs which will load in AX using the Get-AxDllVersions. Download the PowerShell script.  Run this script against all machines running the AX client (RDP/Citrix/end-user) and servers. This is useful to confirm that all DLLs deployed match in file version.

Example 1: Export DLL Versions for Local Machine

.\Get-AxDllVersions.ps1
Example single value:
ComputerName : Server1
FileVersion : 6.3.5000.3084
ProductVersion : 6.3.5000.3084
OriginalName : Microsoft.Dynamics.Retail.TestConnector.dll
FilePath : \DAXDEVMCA1\c$\Program Files\Microsoft Dynamics AX\60\Server\Server1\bin\Connectors
FileDescription :
ProductName : Microsoft Dynamics AX Status : Success

Example 2: Retrieving Multiple Server DLL Versions

.\Get-AxDllVersions.ps1 -ComputerName "Server1","Server2"

Example 3: Export Multiple Server DLL Versions to CSV

.\Get-AxDllVersions.ps1 -ComputerName (Get-Content "ComputerList.txt")  | Export-csv "c:\temp\AX_DLL_Versions.csv" -NotypeInformation

Requirements

The script must be run as an account having Administrator access all computers. The script will skip any offline/inaccessible computers.

Sunday, April 1, 2018

Logoff Disconnected Remote Desktop Sessions using PowerShell

AX 2012 users continue to appear active in AX although they have disconnected from their RDP session.  You can manually open Task Manager on each RDP server and logoff users who have disconnected.  Alternatively, save the following commands as a ps1 file and run it against all RDP servers before checking the Online Users in AX to see who is using AX.


$serverName = "localhost"
$sessions = qwinsta /server $serverName | ?{ $_ -notmatch '^ SESSIONNAME' } | %{
    $item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device"
    $item.SessionName = $_.Substring(1,18).Trim()
    $item.Username = $_.Substring(19,20).Trim()
    $item.Id = $_.Substring(39,9).Trim()
    $item.State = $_.Substring(48,8).Trim()
    $item.Type = $_.Substring(56,12).Trim()
    $item
}
foreach ($session in $sessions) {
    if ($session.Username -ne "" -or $session.Username.Length -gt 1) {
        if ($session.State -eq "Disc") {
            Write-Host ("Logged off {0}" -f $session.Username)

            logoff /server $serverName $session.Id
        }
    }
}

Tuesday, January 23, 2018

Showing Bible Verse when PowerShell is Opened

Using the code created in a previous post in our PowerShell profile, we can have PowerShell display a random Bible quote whenever it is opened.

Steps

  1. Download PSBible-NET.psm1 from Github.
  2. Place the file in %UserProfile%\Documents\WindowsPowerShell\Modules.  You may need to create the folders if they do not already exist.
  3. Create "profile.ps1" in %UserProfile%\Documents\WindowsPowerShell\ and put this inside:

Import-Module "C:\Users\YOUR-USERNAME\Documents\WindowsPowerShell\Modules\PSBible-NET.psm1"

Get-BibleVerse -Random



Every time PowerShell is opened on your machine it will show a random Bible quote!


Keep in mind that the more commands you place inside the profile.ps1, the longer it will take to open PowerShell.

Sunday, January 21, 2018

Search the Internet for Bible References on a Topic

Scraping a Web Site for Bible References

Using the function from the prior post, we can leverage PowerShell to download a web page and parse it for us to find references to scripture.

$requestResult = Invoke-WebRequest -Uri "https://blogs.lcms.org/2018/loving-your-internet-neighbor" -DisableKeepAlive -UseBasicParsing -ErrorAction SilentlyContinue

if ($requestResult.StatusCode -eq 200)
{
    Get-BibleReferences $requestResult.Content | Format-Table -AutoSize
}


Output
Reference    Book    Chapter Verses From To
---------    ----    ------- ------ ---- --
1 Cor. 1:23  1 Cor   1       23     23     
1 PETER 3:15 1 PETER 3       15     15     
John+5:39    John    5       39     39     
John 5:39    John    5       39     39     
Luke 24:27   Luke    24      27     27      


Search the Internet for Bible References on a Topic

So let's take this a bit farther.  Let's say someone sends you an email wanting to know more about the Biblical position on, let's say, heaven and hell.  So you could fire up Google and spend the next half hour looking at the first ten posts on heaven and hell.  But really, we should take what scripture says first, then consider the opinions of others.  This lead me to wanting to look at only the Bible verses relating to Heaven or Hell.

I have built a function which will query Google, read the first page of results then download each of the results, scrape them for Bible references, and return not only the reference but also the Bible text in NET Bible format.  Although it takes about a minute to run it saves a bunch of time!

Get-BibleReferencesOnTopic "Lutheran Heaven Hell" | Select-Object Reference, Text | Export-Csv -Path "References.csv" -NoTypeInformation


Sample output (400 Bible References):


The code is available on Github!

Find Bible Verses using PowerShell

Using regular expressions and PowerShell we can create some code which will parse a Bible verse and then these values can be used in other ways.  In the function below I've used the .NET RegEx class to read Bible verses, which can be read from a text/CSV file into a variable.  It will find Bible verses within other text too.

function Get-BibleReferences {
    <#
        .SYNOPSIS
            Finds scripture references within the provided text.

        .DESCRIPTION
            Uses regular expressions to find references to the Bible and returns them as a set.

        .PARAMETER Text
            The text to parse for scripture references.

        .EXAMPLE
            PS C:\> Get-BibleReferences "Titus 3:1  Really any text can be here -- 1 Timothy 2:1-2 -" | Format-Table -AutoSize

            Reference       Book      Chapter Verses From To
            ---------       ----      ------- ------ ---- --
            Titus 3:1       Titus     3       1      1     
            1 Timothy 2:1-2 1 Timothy 2       1-2    1    2

        .NOTES
            Dag Calafell
            01.20.2018

        .LINK
            https://dynamicsax365trix.blogspot.com/
    #>
    [CmdletBinding(DefaultParameterSetName="Default")]
    param(
        [Parameter(ParameterSetName="Default",
            ValueFromPipelineByPropertyName=$true,
            Mandatory=$true,
            Position=0)]
        [ValidateNotNullOrEmpty()]
        [string[]]$text
    )

    $regex = new-object System.Text.RegularExpressions.Regex("(?(?:(?:[123]|I{1,3})\s*)?(?:[A-Z][a-zA-Z]+|Song of Songs|Song of Solomon)).?\s*(?1?[0-9]?[0-9]):\s*(?\d{1,3})(?:[,-]\s*(?\d{1,3}))*", [System.Text.RegularExpressions.RegexOptions]::MultiLine)
    $regexMatches = $regex.Matches($text)

    foreach ($match in $regexMatches)
    {
        $groups = $match.Groups
        $book         = $groups[1].Value
        $chapter      = $groups[2].Value
        $fromVerseNum = $groups[3].Value
        $toVerseNum   = $groups[4].Value

        $object = New-Object –TypeName PSObject
        $object | Add-Member –MemberType NoteProperty –Name Reference –Value $groups[0].Value
        $object | Add-Member –MemberType NoteProperty –Name Book –Value $book
        $object | Add-Member –MemberType NoteProperty –Name Chapter –Value $chapter

        if ($groups[4].Success)
        {
            $object | Add-Member –MemberType NoteProperty –Name Verses –Value ("{0}-{1}" -f $fromVerseNum, $toVerseNum)
            $object | Add-Member –MemberType NoteProperty –Name From –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name To –Value $toVerseNum
        }
        else
        {
            $object | Add-Member –MemberType NoteProperty –Name Verses –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name From –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name To –Value ""
        }

        # Return the info
        $object
    }
}

# Example
Get-BibleReferences "Titus 3:1  Really any text can be here -- 1 Timothy 2:1-2 -" | Format-Table -AutoSize

Output
Reference       Book      Chapter Verses From To
---------       ----      ------- ------ ---- --
Titus 3:1       Titus     3       1      1      
1 Timothy 2:1-2 1 Timothy 2       1-2    1    2 

This code is part of a larger script I'm working on to take a file of scripture references and return the full text to aid me in developing Bible studies.

Credit goes to RegexLib for the starting regex that I modified capture the data into groups.  Many times it is easier to find something to start with and extend it to what is desired.