serving the solutions day and night

Pages

Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Saturday, April 11, 2020

Docker Commands

Verify the application exist inside the windows container,  connect to the cmd of the "frontendapiwin" container using the docker exec command
1. C:\> docker exec -it 64a61d823b28  cmd
           - "64a61d823b28 " - docker container id
           - It should take us inside the container.
     Run the powershell inside the "frontendapiwin" the container 
     1.1 C:\inetpub\wwwroot> powershell 
            Using Powershell to manage application web configuration
           1.1.1 PS C:\inetpub\wwwroot> gc .\web.config   or  gc web.config 
                       - Gets the content (gc) of the item at the specified location (C:\inetpub\wwwroot\web.config)
         > [xml]$web = gc .\web.config
                         -  The [xml] casts the variable as an XML object.
                         - <?xml>
                            <configuration>
                                    <appsettings>
                                             <add key="DBCon" value="db value."/>
                                             ...
                                     </appsettings>
                                    ...
                             </configuration>
         > $web.configuration.appSettings.SelectNodes("add[@key='DBCon']")
                           - display DBCon key value
                           -  key                 value
                              -----                 -------
                               DBCon        db value.
         > $key=$web.configuration.appSettings.SelectNodes("add[@key='DBCon']")
                           - assign to key
         > $key
                           - another way display DBCon key value
         > $key.SetAttribute("value","db value changed.")
                           - change DBCon value
         > $web.save(".\web.config")
                           - save file
         > exit  => exit from powershell
           > exit  = exit from container

Thursday, November 9, 2017

PowerShell - MS Dynamics CRM CRUD Operations

Please refer Read App.Config -
Please refer Connect MS Dynamics CRM

$crm_service = Invoke-CRMConn -OrganizationUrl $crm_organization_url -UserName $crm_username -Password $crm_password

## 1. To test the connection, check who you are:
function WhoAMI() {
    $request = new-object Microsoft.Crm.Sdk.Messages.WhoAmIRequest
    $crm_service.Execute($request)

    <#
    UserId         : 0dc96a70-952d-e611-80f0-0050568c6c1c
    BusinessUnitId : a6099f28-952d-e611-80ec-0050568c6c1c
    OrganizationId : 0d67a31e-952d-e611-80ec-0050568c6c1c
    ResponseName   : WhoAmI
    Results        : {[UserId, 0dc96a70-952d-e611-80f0-0050568c6c1c], [BusinessUnitId, a6099f28-0caf-e611-80ec-0050568c6c1c],
                     [OrganizationId, 0d67a31e-0caf-e611-80ec-0050568c6c1c]}
    ExtensionData  : System.Runtime.Serialization.ExtensionDataObject
    #>
}


PowerShell - Connect MS Dynamics CRM

## create crm connction function, call this function with arguments  OrganizationURL, UserName, Password
function Invoke-CRMConn {
    ## pass arguments OrganizationURL, UserName, Password
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true)] [string]$OrganizationURL,
        [Parameter(Position=1, Mandatory=$true)] [string]$UserName,
        [Parameter(Position=2, Mandatory=$true)] [string]$Password
    )

    ## get directory path
    $current_directory =  $(Get-Item ($MyInvocation.ScriptName)).DirectoryName
   
    ## load crm & xrm dll
    $xrm_dll = $current_directory + "/microsoft.xrm.sdk.dll"
    $crm_dll = $current_directory + "/microsoft.crm.sdk.proxy.dll"
    [void][System.Reflection.Assembly]::LoadFile($xrm_dll)
    [void][System.Reflection.Assembly]::LoadFile($crm_dll)
    [void][System.Reflection.Assembly]::LoadWithPartialName("system.servicemodel")

    ## create credentials
    $clientCredentials = new-object System.ServiceModel.Description.ClientCredentials
    $clientCredentials.UserName.UserName = $UserName
    $clientCredentials.UserName.Password = $Password
   
    ## create  organization service proxy 
    $service = new-object Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy($OrganizationURL, $null, $clientCredentials, $null)
   
    ## use default crm time out or set 5 minutes of timeout.
    #$service.Timeout = new-object System.Timespan(0, 5, 0)

    ## return service
    return $service
}

## test the service using who i am request
$service1 = Invoke-CRMConn -OrganizationURL "https://dns.com/XRMServices/2011/Organization.svc" -UserName 'test@dns.com' -Password ABC123!@#'
$request = new-object Microsoft.Crm.Sdk.Messages.WhoAmIRequest
$service1.Execute($request)

<#
UserId             : 123ddfa0-9534-f234-8780-78900568c2fc0
BusinessUnitId : 456a043cf-0c34-e234-878c-78900568c6c1c
OrganizationId : 7895b3c2-0c34f-e234-878c-7890568c6c1c
ResponseName   : WhoAmI
Results        : {[UserId, 123ddfa0-952d-f234-8780-7890568c6c1c], [BusinessUnitId, 456a043cf-0caf-e234-8780-7890568c6c1c],
                 [OrganizationId, 7895b3c2-0caf-e234-8780-7890568c6c1c]}
ExtensionData  : System.Runtime.Serialization.ExtensionDataObject
#>

PowerShell - Read App.config

## get current directory
$current_directory =  $(Get-Item ($MyInvocation.MyCommand.Path)).DirectoryName

## $MyInvocation.MyCommand.Path won't work if you call inside the function.
## so use ScriptName to get currect directory
$current_directory =  $(Get-Item ($MyInvocation.ScriptName)).DirectoryName

## path of config file
$app_config_path = $current_directory + "\App.config"

## reading configuration file
$xml_doc = [Xml](Get-Content $app_config_path)

## get appsettings value
$app_nodes = $xml_doc.configuration.appSettings.add
$url = ($app_nodes | where {$_.key -eq 'Url'}).value
$username = ($app_nodes | where {$_.key -eq 'UserName'}).value
$password = ($app_nodes | where {$_.key -eq 'Password'}).value

## get dbconnection setting
$con_nodes = $xml_doc.configuration.connectionStrings.add
$db_con = ($con_nodes | where {$_.name -eq 'DBConnectionString'}).connectionString

## display value
Write-Host  $url
Write-Host  $username
Write-Host  $password
Write-Host  $db_con

<#
App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Url" value="https://dns.com/Services/2017/Organization.svc" />
    <add key="UserName" value="mak@dns.com"/>
    <add key="Password" value="ABC123456!@#"/>
  </appSettings>
  <connectionStrings>
    <add name="DBConnectionString" connectionString="Data Source=MAK12345.dns.com,1044;Initial Catalog=COS_SVC;User ID=root; Password=DEF456$%^;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>
#>

Wednesday, November 8, 2017

PowerShell Dictionary

1.Create empty dictionary
$contacts = @{}

2.Add key,value item
## Way 1:
$key = 1
$value = 'CRM'
$contacts.add( $key, $value )

## Way 2:
$contacts.add( 2, 'C#' )

## Way 3:
$contacts[3]='.NET'

## Way 4 - Keys are just strings
$contacts = @{
    'full name' = 'C# Code'
    '#' = 1234
}
$contacts['full name'] # OR  $contacts.'full name' OR $key = 'full name' $contacts.$key
<#
    C# Code
#>

## Creating dictionary with values
$contacts = @{
    1 = 'CRM'
    2 = 'C#'
    31 = '.NET'
}
$contacts[4]='Azure'
$contacts[5]='Cloud'

## print contact dictionary
$contacts
<#
    Name                           Value                                                                                                   
    ----                           -----                                                                                                   
    5                              Cloud                                                                                                   
    4                              Azure                                                                                                   
    31                             .NET                                                                                                     
    2                              C#                                                                                                       
    1                              CRM
#>

$contacts_age = @{
    1 = 10
    2 = 20
    3 = 25
}

Tuesday, November 7, 2017

PowerShell – running scripts is disabled on this system.

When i execute powershell script from Windows PowerShell ISE, i got error, means PowerShell execution policy doesn't allow to run script.

Error
File TaskYearApp.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess

First i want to see what's my execution policy settings, i run the below commands

PS G:\> get-executionpolicy
Restricted

So PowerShell execution policy is default set to Restricted.

PS G:\> get-executionpolicy -List

  Scope                    ExecutionPolicy
  ---------------       ----------------
  MachinePolicy       Undefined
 UserPolicy              Undefined
 Process                   Undefined
 CurrentUser            Undefined
 LocalMachine         Undefined
 
 List of four different execution policies in PowerShell

  Restricted – No scripts can be run.
  AllSigned – Only scripts signed by a trusted publisher can be run.
  RemoteSigned – Downloaded scripts must be signed by a trusted publisher.
  Unrestricted – All Windows PowerShell scripts can be run.

Workaround solution is, you can change the PowerShell execution policies with Set-ExecutionPolicy cmdlet. Run PowerShell as administrator, Give it the remote signed execution policy which allows to execute that script

PS G:\> powershell -ExecutionPolicy RemoteSigned
- it will take time to set the policy

OR

PS G:\> Set-ExecutionPolicy RemoteSigned