Author Posts

October 5, 2017 at 12:58 am

I have a .ps1 file, I could change it to .txt because it is just a file that has a snip of code I want to append to the (all users all hosts) profile. The "setup" script creates the profile file if it does not exist then adds the code to the end of the profile file.

What I want to do is prevent the "setup" script from appending the profile file again and again. I only want it appended once. I cant seem to get the matching to work correctly. Anyone have any advice on how I can append the get-content from my snip file to the profile file only once?

my snip looks like this. Hash tags don't seem to post so I omitted them.

region snip
the code
endregion snip

October 5, 2017 at 9:21 am

Hi Kevin,

Could you post an example?
My English isn't that good (still improving haha), and don't quite understand what you are trying to accomplish.
For what I am reading, I understand you have a certain setup file with a script or code in there.
The setup script creates a profile file and adds a certain code from another? file to the end of the profile file?

I can't quite seem to understand why this would loop, if not specified to loop.
So an example might help me understand better and then I might be able to help you.

Kind regards,
Ramon Schouten

October 5, 2017 at 1:46 pm

Thank you. I created a module to share with my team. I created a internal repository and created a "setup" script that will add the trusted repository. The repository will reside on a UNC share. The "setup" script will then install the repository and create a profile.ps1 file if it does not already exist then append a code snip from another text file. The snip is just code that will update the profile. The problem is if my team tries to run the "setup" script a second time or more, the snip will be appended to the profile again and again. I want to avoid that.

Snip.ps1 (I might rename this to Snip.txt).
This is what gets copied to the profile file.

#region - Do NOT Delete this section ###
#--------------------------------------#
# This section is added to the AllUsersAllHosts profile
# This is added to the end of the profile folder to update
# exp modules that reside in the exp module repository

$ModuleList = find-module -Repository Companymodule
foreach($Module in $ModuleList){
    
    if(Get-InstalledModule -Name $module.Name -ErrorAction SilentlyContinue){
        Update-Module -Name $module.Name
    }
}

#-----------------------------------------#
#endregion - Do NOT Delete this section ###

My "Setup" script to add the repository, install the module and profile

#Requires -RunAsAdministrator

Import-Module PowerShellGet
#check dependencies for module and powershell version that should be installed
$Path = '\\Server\Folder\PowerShell\CompanyModule'

$repo = @{
    Name = 'CompanyModule'
    SourceLocation = $Path
    PublishLocation = $Path
    InstallationPolicy = 'Trusted'
}

Register-PSRepository @repo 

Install-Module -Name Companytools -Repository Companymodule


#create the all user all host profile if not already present
if(-not (Test-Path $profile.AllUsersAllHosts)){
    New-Item -Path $profile.AllUsersAllHosts -ItemType File -Force | Out-Null
}

$code = Get-Content -Path '\\Server\Folder\PowerShell\CompanyScripts\snip.ps1' -raw
$profileContent = Get-Content $profile.AllUsersAllHosts -raw

if(-not ($profileContent -like $code)){
    Add-Content -Path $profile.AllUsersAllHosts -Value $code -Force | Out-Null
}

The last if statement does not work. I tried using match as well.

October 5, 2017 at 1:57 pm

Thank you. I created a module to share with my team and I want to make it easy to share as well as update. I am writing a "Setup" script to setup the trusted repository, install the module, then create or append a PowerShell profile to run the update command. The update command is not complete yet, but the problem I have is if someone runs the "Setup" script a second or more times, the custom profile code gets appended to the profile again. I need the "Setup" script to skip that step if the code already exists in the profile, or delete it and re-create it if I ever update it.

Setup script that adds the trusted repository, installs the module, handles the profile.

#Requires -RunAsAdministrator

Import-Module PowerShellGet
#check dependencies for module and powershell version that should be installed
$Path = '\\Server\Folder\PowerShell\CompanyModule'

$repo = @{
    Name = 'CompanyModule'
    SourceLocation = $Path
    PublishLocation = $Path
    InstallationPolicy = 'Trusted'
}

Register-PSRepository @repo 

Install-Module -Name Companytools -Repository Companymodule


#create the all user all host profile if not already present
if(-not (Test-Path $profile.AllUsersAllHosts)){
    New-Item -Path $profile.AllUsersAllHosts -ItemType File -Force | Out-Null
}

$code = Get-Content -Path '\\Server\Folder\PowerShell\CompanyScripts\snip.ps1' -raw
$profileContent = Get-Content $profile.AllUsersAllHosts -raw

if(-not ($profileContent -like $code)){
    Add-Content -Path $profile.AllUsersAllHosts -Value $code -Force | Out-Null
}

snip.ps1

#region - Do NOT Delete this section ###
#--------------------------------------#
# This section is added to the AllUsersAllHosts profile
# This is added to the end of the profile folder to update
# exp modules that reside in the exp module repository

$ModuleList = find-module -Repository CompanyModule
foreach($Module in $ModuleList){
    
    if(Get-InstalledModule -Name $module.Name -ErrorAction SilentlyContinue){
        Update-Module -Name $module.Name
    }
}

#-----------------------------------------#
#endregion - Do NOT Delete this section ###

October 5, 2017 at 3:08 pm

Hi Kevin,

It is a easy to fix issue.
First, to have a more definitive answer to what was happening I added a simple "else" condition:

if(!($profileContent -like $code)){
    Write-Host "Adding content to file '$profile.AllUsersAllHosts'"
    Add-Content -Path $profile.AllUsersAllHosts -Value $code -Force | Out-Null
} Else {
    Write-Host "'$profile.AllUsersAllHosts' already contains content"
}

Notice I also changed if(-not ($profileContent -like $code)) to 'if(!($profileContent -like $code))'.
This is not wrong, i just find it easier.

After this it got clear to me that it was looking if the entire file was exactly like $code.
So implementing the following didn't loop anymore:

if(!($profileContent -like "*$code*")){
    Write-Host "Adding content to file '$profile.AllUsersAllHosts'"
    Add-Content -Path $profile.AllUsersAllHosts -Value $code -Force | Out-Null
} Else {
    Write-Host "'$profile.AllUsersAllHosts' already contains content"
}

So all I did was change '-like $code' to '-like "*$code*"'

Since I have simplified your script to test it quickly, the first time the output was:
Adding content to file '$profile.AllUsersAllHosts'

And the second time running it was:
'$profile.AllUsersAllHosts' already contains content

October 5, 2017 at 3:12 pm

Err... I don't know what happened to my previous reply.

But it is an easy fix.

Just change

if(-not ($profileContent -like $code)){
    Add-Content -Path $profile.AllUsersAllHosts -Value $code -Force | Out-Null
}

to

if(-not ($profileContent -like "*$code*")){
    Add-Content -Path $profile.AllUsersAllHosts -Value $code -Force | Out-Null
}

This way it will look if it contains that certain string and not if it exists of that certain string.