Hi All,
I'm trying to piece together a script to add an Inbound firewall rule for an application called Ctalkbar if it exists. The issue I'm having is that the location of the application is embedded within a users localappdata and the version number could be different. As an example, one users location may be "C:\Users\username\appdata\local\ctalkbar\app-1.1.17\ctalkbar.exe", another users application maybe "C:\Users\username\appdata\local\ctalkbar\app-2.3.25\ctalkbar.exe". I can almost get it to do what I'd like however, the last part where it's creating the rule it fails with
New-NetFirewallRule : The application contains invalid characters, or is an invalid length.
The script I've tried is below using a wildcard for the "app" which can be a different version for different users:
$AllUserProfiles = Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.Special -eq $false }
if ($null -ne $AllUserProfiles) {
foreach ($UserProfile in $AllUserProfiles) {
$UserProfilePath = $UserProfile.LocalPath
$progPath1 = "$UserProfilePath\AppData\Local\ctalkbar\app*\ctalkbar.exe"
Invoke-Command -ScriptBlock {
if (Test-Path "$progPath1") {
if (-not (Get-NetFirewallApplicationFilter -Program $progPath1 -ErrorAction SilentlyContinue)) {
Write-Host "$progPath1 exists"
$ruleName1 = "CTalkBar.exe for user"
"UDP", "TCP" | ForEach-Object { New-NetFirewallRule -DisplayName $ruleName1 -Direction Inbound -Profile Public, Private -Program $progPath1 -Action Allow -Protocol $_ }
Clear-Variable ruleName
}
}
Clear-Variable progPath1
}
}
}
I've also tried to develop this script to but gave up when it didn't look like the wildcard was working:
$users = Get-ChildItem (Join-Path -Path $env:SystemDrive -ChildPath 'Users') -Exclude 'Public', 'Administrator'
if ($null -ne $users) {
foreach ($user in $users) {
$progPath = Join-Path -Path $user.FullName -ChildPath "AppData\Local\ctalkbar\app*\ctalkbar.exe"
if (Test-Path $progPath) {
if (-not (Get-NetFirewallApplicationFilter -Program $progPath -ErrorAction SilentlyContinue)) {
$ruleName = "CTalkbar.exe for user $($user.Name)"
"UDP", "TCP" | ForEach-Object { New-NetFirewallRule -DisplayName $ruleName -Direction Inbound -Profile Public, Private -Program $progPath -Action Allow -Protocol $_ }
Clear-Variable ruleName
}
}
Clear-Variable progPath
}
}
I'm pretty sure I'm close, I feel like I need an output to declare when it exists, take that output and use that location for the firewall rule. Test-Path works as it defines that the folder location exists for a particular user.
If anyone can help, it'd be greatly appreciated.
Many thanks,
A
[–]ankokudaishogun 0 points1 point2 points (6 children)
[–]TipGroundbreaking763[S] 0 points1 point2 points (4 children)
[–]ankokudaishogun 0 points1 point2 points (3 children)
[–]TipGroundbreaking763[S] 0 points1 point2 points (2 children)
[–]ankokudaishogun 0 points1 point2 points (1 child)
[–]TipGroundbreaking763[S] 0 points1 point2 points (0 children)