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.
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.
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.
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 ###