-1

Environment:

  • System Center Configuration Manager 1810
  • Workstations = Windows 10 1709 / Windows 7 SP1
  • Application spécific with add-on script to accomplish the tasks

Way to accomplish it:

  1. Deploy package without licence
  2. Push licence file

    1. Stop service
    2. Read licence with SCCM tack and push it if necessary
    3. Start service

Hundreds of computers are affected and the editor doesn't submit a guideline to accomplish it without rebooting computers without prompting end-users.

We are using SCCM to deploy and check packages (WMI query, registry, ...). We can use, powershell to query more objects, like reading the licence file to check if it is the good one.

Reading licence is done this way:

if ((Get-Content "C:\Program Files\XXX\X.LIC") -contains serial_no=XXXXX")) {
    Write-Host "License OK"
}

If the licence, isn't the good one, a little is launched on the workstation (somewhere in a folder like C:\Windows\CCMCACHE\a) like

If (Test-Path ("C:\Program Files (x86)\NetSupport\NetSupport School"))
{
    If (Test-Path ("C:\Program Files (x86)\XXX\X.LIC"))
    {
        Rename-Item -Path "C:\Program Files (x86)\XXX\X.LIC" -NewName  ("X.LIC." + (Get-Date).ToString("yyyyMMdd")) -Force
    }
    Copy-Item -Source $PSScriptRoot\X.LIC -Destination ("C:\Program Files (x86)\XXX") -Force
}
ElseIf(Test-Path ("C:\Program Files\XXX"))
{
    If (Test-Path ("C:\Program Files\XXX\X.LIC"))
    {
        Rename-Item -Path "C:\Program Files\XXX\X.LIC" -NewName  ("X.LIC." + (Get-Date).ToString("yyyyMMdd")) -Force
    }
    Copy-Item -Source $PSScriptRoot\X.LIC -Destination ("C:\Program Files\XXX") -Force
}

Do I need PowerShell v3? How can i do it with PSv2 ?

Yann F.
  • 91
  • 3
  • 14
  • because you are not doing any validation for the license in the copy statement. Club the copy part inside your if statement – Ranadip Dutta May 03 '19 at 10:53
  • It's completely unclear to me what problem you're facing. Please create a [mcve] that demonstrates your problem, test-run that code, then [edit] your question and post *that* code along with a more detailed description of desired and actual behavior as well as all errors you're getting. – Ansgar Wiechers May 03 '19 at 11:39
  • If you are asking if `Copy-Item` is a thing in PS v2, I dont think it is. MS documentation only goes as far back to v3. But you can always use the native cmd command `copy` to transfer your license file. Those work from powershell too and hence can be used in scripts. – Sid May 03 '19 at 11:41
  • @RohinSidharth `Copy-Item` has existed since PowerShell v1. – Ansgar Wiechers May 03 '19 at 13:22
  • Thanks for all your comments. I try do edit my question and give more explainations about System Center Configuration Manager and what i attempt to do on worksations with PS2 – Yann F. May 07 '19 at 14:11

1 Answers1

0

First of all, the Copy-Item was wrong -> -Path instead of -Source

And, in Windows PowerShell 2.0, $PSScriptroot is valid only in script modules (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.

So, i do it like this :

$scriptpath = split-path -parent $MyInvocation.MyCommand.Definition
If (Test-Path ("C:\Program Files (x86)\XXX"))
{
    If (Test-Path ("C:\Program Files (x86)\XXX\X.LIC"))
    {
        Rename-Item -Path "C:\Program Files (x86)\XXX\X.LIC" -NewName  ("NSM.LIC." + (Get-Date).ToString("yyyyMMdd")) -Force
    }
    Copy-Item -Path ($scriptpath + "\X.LIC") -Destination ("C:\Program Files (x86)\XXX") -Force
}
ElseIf(Test-Path ("C:\Program Files\XXX"))
{
    If (Test-Path ("C:\Program Files\XXX\X.LIC"))
    {
        Rename-Item -Path "C:\Program Files\XXX\X.LIC" -NewName  ("X.LIC." + (Get-Date).ToString("yyyyMMdd")) -Force
    }
    Copy-Item -Path ($scriptpath + "\X.LIC") -Destination ("C:\Program Files\XXX") -Force
}

Thanks for your help ^^

Yann F.
  • 91
  • 3
  • 14
  • i could use ${env:ProgramFiles(x86)} and $env:ProgramFiles or the variable %Processor_Architecture% (AMD64/x86) too ^^ – Yann F. May 14 '19 at 07:46