Quantcast
Channel: Windows PowerShell forum
Viewing all 21975 articles
Browse latest View live

Uninstalling all versions with Powershell and understanding Uninstall Stings

$
0
0

Right, this is quite similar to a previous query..

I am writing a script and I want it to check an MSI to see if any version is installed already and if it is, remove all versions already installed and install the new one..

I can run

msiexec.exe /x $FilePath /quiet

Which will uninstall that version if its already installed but not other versions of the software.. I was advised by a peer I was best using the uninstall string to remove all versions. So to find the Uninstall string for a particular product I wrote..

Get-ChildItem  'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\' | foreach { gp $_.PSPath } | ? { $_ -match "Blah Blah Blah" } | select UninstallString

This brings out the uninstall string for the searched product that's installed.. Which shows up in this case something like

MsiExec.exe /I{XXXX9XXX-99X9-9X99-X999-9XX999999999}

Running that doesn't work though, it doesn't uninstall anything.. Why isn't it uninstalling the versions installed?

Also, I found that uninstall string by searching "Product Name" and im trying to automate this process when I pick up the MSI.. Is there anyway when I pick up the MSI to have the system search the registry and recognize if any versions of that product are already installed? If so, then to uninstall them?

I wrote -

$Namex = Get-ItemProperty $filepath | select name
Get-ChildItem 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\' | foreach { gp $_.PSPath } | ? { $_ -match "$namex" } | select UninstallString

The value of $Namex is for argument sake 'Blah Blah Blah 8.3.5.0 installer.msi' and using the above code doesn't return anything but say I cut the 8.3.5.0 installer.msi part off and manually wrote -

ChildItem 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\' | foreach { gp $_.PSPath } | ? { $_ -match "Blah blah Blah" } | select UninstallString
     

it would return the uninstall string..

Any Ideas or suggestions how this process could be automated?



Email results of a script

$
0
0

I'm currently learning powershell and i'm trying to create a small script to export my VM in hyper-v then I would like to send an email with either a subject of successful or failure. I have the basic export command but i'm not sure how to verify to see if it worked or not.

$VM = "VM-1"
$ExportPath = "f:\VM-Backup"
Export-VM -Name $VM -Path $ExportPath

 

Deletion of the duplicates in Excel file using Powershell

$
0
0

Hello,

Could you please give me a hand with the deletion of the duplicates in Excel file? I need to choose two columns. My file contains headers. How can I do that with Powershell?

Many thanks.

why -replace doesn't work

$
0
0

Hello,

I'm trying to delete some text from my SCV file and I'm doing something like this:

csv = Get-Content "$PSScriptRoot\$file"
    
    $csv | ForEach-Object { $_ -replace '"', "" } | Set-Content -path "$PSScriptRoot\$($file.name)"

    $csv = Get-Content "$PSScriptRoot\$file"

    $csv | ForEach-Object { $_ -replace "CREATE TABLE permission denied in database 'tempdb'.", "" } | Set-Content -path "$PSScriptRoot\$($file.name)"
    $csv | ForEach-Object { $_ -replace "Line 1", "Line 1,CREATE TABLE permission denied in database 'tempdb'." } | Set-Content -path "$PSScriptRoot\$($file.name)"

The thing is that this string

$_ -replace "CREATE TABLE permission denied in database 'tempdb'.", ""

doesn't work and I can't figure out why. It simply doesn't change the text with the empty value as I'd like it.

Tell me please what I'm doing wrong?

Thank you.

Best way to grab data from Oracle SQL developer powershell

$
0
0

Hello,  I am new to powershelgl and would appreciate some assistance on something I"m trying to do.

My Objective is to automate my report.  my is more or less as follows:  

1.  execute a sql dev query to get data

2. copy data into excel sheet

3. update necessary pivot tables on excel sheet

4. email appropriate parties about report update.

Right now,  I do not know what is the best/simplest method to execute #1 and transfer to #2 using power shell.

Is it possible to:

a) write a powershell script that will run my query and somehow export the results to a spreadsheet (all using power shell scripts)

or

b) write a powershell script that will simply run the query, and let my query to the automatic export (Right now, my sql query uses the 'spool' command to auto export the results as a csv)

or

c) another easier option?

With either ways, how do I start?  I do not know even how to connect power script oracle sql dev.


editing local GPO by powershell

$
0
0

Hello,

I need to edit local group policy object using Powershell but I can't do that. I have tried to google but didn't find anything really useful. I've read that I can do that somehow via edition of the Registry but how?

I need to amend the following GPOs:

gpedit.msc > Computer Configuration > Windows Settings > Security settings > Local Policies > Security Options

accounts:administrator account status - disabled

account rename administrator account - any value

Thanks.

Remove Local user like "x*"

$
0
0

Hi Folks!,

I need such special guys which can help me with the following trouble.

I have made a script which knows over to all Computer that starts with X name and looks for a local user which call "xx" and delete it, in additional knows to catch + try for error.

The challenge is that I don't know what is the exact local User-Name in my computers but one thing know certainly that it starts with tvu computer

So this is the Script which knows to delete a local user if I state permanent name:

$ComputerList = Get-ADComputer -Searchbase "DC=tmt,DC=telital,DC=com" -Filter {Enabled -eq $true -and name -like "xxx*" }
$output = ""

Foreach ($machine in $ComputerList.Name)

{
    Try

    {

              $ComputerMember = [adsi]"WinNT://$machine"

           $MESSAGE = $ComputerMember.Delete("User", "tvu*")
        $output = $output + $ComputerName +"SUCCESS `r`n"
    }

    Catch

    {
    $Errormessage = $_.Exception.Message
    $result = "($machine) ($ErrorMessage)"


   if ($ErrorMessage -like '*Exception calling "Delete"*')
    {
     $output = $output + $machine + " User Name NOT Found`r`n"

    }

    elseif($ErrorMessage -like "*Testing connection to computer*")
    {
     $output = $output+$machine+ "  Isn't Avalible`r`n"
    }
    else
    {
          $output = $output + $result
    }
    }
}
$output > c:\temp\res.txt

the script works like charm, but I would like that it will delete any user that start with 'tvus_*'.

Actually, I succeeded to do that in a different way, If I am run this separately without involving  into above script it works, But I would like to combine between the script:

* Note that it works because I stated a specific computer name:

$ComputerMember = [adsi]"WinNT://zubidubi"
        $Users = $ComputerMember.Children  | where {$_.SchemaClassName  -eq 'user'}
        $users.Name

         foreach ($user in $users.Name)

        {

        if ($users.Name -like "tvus_*")

        {
         $ComputerMember.Delete("User",$user)

        }



        } 
I have thought about 2 ForEachs but unfortunately, I do not know how to do that...


My Website:www.Pelegit.co.il Mcitp /Mcsa 2012



Nested If and Else Statements

$
0
0

I have a script that was working fine until I needed to add more logic to it.  The end goal is to run either a x86 or x64 command on a responding machine.

I needed to add another if / else section under line 13, to wake computers that were off or asleep.

For computers that are not responding at line 4, the script works fine.  But for computers that are responding at line 4, they are running the command, twice.  Once from line 7 or 10, and again from line 21 or 25.

I've been digging to find a sample script that does something like what I'm trying to do, but haven't had any luck finding anything where the context is close enough to make sense to me.

I'm sure I have something in my logic screwed up.  I want to fix (and understand it) correctly, instead of being lazy and adding breaks after the Invoke-Command lines. :)

foreach ($computer in $computers)

{
    if (Test-Connection $computer -count 2 -Quiet) {
    $a = Invoke-Command -computer $computer -ScriptBlock { $env:PROCESSOR_ARCHITECTURE}
        if ($a -eq 'AMD64') {
        Invoke-Command -ScriptBlock {command}
        }
        else {
        Invoke-Command -ScriptBlock {command}
        }
    }
    else {
    Invoke-Expression "$WakeScript $WakeArgs"
    Invoke-Expression "$WaitScript"
        if (Test-Connection $computer -count 2 -Quiet) {
        ## THIS SHOULD ONLY BE EXECUTED IF THE FIRST if CONDITION WAS FALSE
        $a = Invoke-Command -computer $computer -ScriptBlock { $env:PROCESSOR_ARCHITECTURE}
            if ($a -eq 'AMD64') {
            ## THIS SHOULD ONLY BE EXECUTED IF THE FIRST if CONDITION WAS FALSE
            Invoke-Command -ScriptBlock {command}
            }
            else {
            ## THIS SHOULD ONLY BE EXECUTED IF THE FIRST if CONDITION WAS FALSE
            Invoke-Command -ScriptBlock {command}
            }
        }
        else {
        ## THIS SHOULD ONLY BE EXECUTED IF THE FIRST if CONDITION WAS FALSE
        Write-Host "$Computer - STILL NO RESPONSE - NO COMMAND SENT" -foregroundcolor Red
    }
}
Write-Host "Completed"

Thanks!

-Matt


There's no place like 127.0.0.1


Getting Volume Shadow copy information on Multiple servers

$
0
0

Hi,

I am a beginner in power shell and trying to write a power shell script to get the shadow copy drive space information on list of servers. I need the output in csv format with Servername, drive info like free space and used. I tried writing below script but its not giving me the desired output.

LogDate = get-date -f ddmmyyyy

$Servers = Get-Content -Path "C:\temp\machinelist.txt"

ForEach ($Server in $Servers) {vssadmin list shadowstorage} 

I tried the below get-WMIobject command but its not giving me the desired output either.

Get-WMIObject Win32_ShadowStorage | Select-Object @{n=’UsedSpaceGB’;e={[math]::Round([double]$_.UsedSpace/1GB,3)}}, Volume

Any help will be appreciated.

Thanks

Manish



Msharma

Powershell Get-ADGroupMember Size Limit

$
0
0

Get-ADGroupMember -identity "Applications" -recursive|
Where-Object {$_.distinguishedName -like "*OU=Apps,OU=Security*" }| 
Select Name,SamAccountName |
Sort -Property Name |
Export-csv -path C:\Members.csv -NoTypeInformation

Purpose: I'm attempting to list users accounts who belong to a specific group but only those users from a specified OU.

The script above ran perfectly yesterday when I wrote it, producing exactly what I need.  However, when I came into work today, and working in the same session of Powershell, I received the following:

Get-ADGroupMember : The size limit for this request was exceeded
At line:1 char:1

I then closed the session and attempted to run this script again but keep receiving the same error.  I don't want to change the ADWS settings to extend the size, is there an alternative or some modification I can do to achieve the same result?

Please advise.  Thanks.

How to get the caller Function Name in the called function in PS?

$
0
0

Hi,

Is there a way to get the caller Function Name in the called function in PS?

function A($args) {

   #I want to know the instance is called from function B here.

}

function B {

   A @args

}


Komal.

How to Share a variable across the functions in a module

$
0
0

Hi,

I have a module where I want to share a variable across the functions. Please note that, the module can be called from different contexts at a time. So, I think global variable might not be a choice.

Appreciate your help :)


Komal.

Tracking/Debugging Memory Leaks in powershell.

$
0
0

Hi,

I would like to explore on if there is a tool/script to track the memory leaks when a powershell module runs?

Should we really worry about the memory leaks in powershell or the underlying .Net Framework garbage collector does takes care and we are free to use the objects as we like.

If there are any good documentation or articles, please share.

Thanks


Komal.

Unable to get Event Log data using Windows Powershell for Events under Microsoft Container

$
0
0

Hi All,

I'm not sure what is wrong here, I was trying to query an event log container from powershell to get the list of events logged in a specific local machine.

I wanted to query the events in the Event Log "Microsoft-Windows-Windows Remote Management/Operational"

But when i do it with Powershell it does not work.

I used the command Get-Eventlog Microsoft-Windows-Windows Remote Management/Operational

Also tried Get-Eventlog "Microsoft-Windows-Windows Remote Management/Operational" (With quotes)

Both don't work and they give the below error:

But when we use Get-Eventlog Application this works as expected.

Can any one let me know what is the issue here ?

Add shared printer from Powershell, driver cannot be retrieved from the server

$
0
0

When trying to add a shared printer from Powershell via : 

Add-Printer -ConnectionName '\\server-host\shared-printer-name'

The following error occurs : 

Add-Printer : The driver needed to connect to this print share cannot be retrieved from the server and must be manually installed

As I understand the needed driver is not in the local driver store and it does not get downloaded from the printer server for some reason.

The 'printer server' is basically just another windows box where I shared a printer.

Is the driver supposed to be downloaded at all from the printer server ?

If so, am I sharing it wrongly or there are some additional steps to be performed for getting the driver installed ?

When adding the printer from Settings, it detects the driver is missing and offers to download it (I think it uses SMB2 to achieve that) :

https://postimg.org/image/oc6gw86hf/

Wondering whether it is possible to the same in Powershell




How come setting the font size doesn't do anything?

$
0
0

I have really bad vision, and I set the font size in Properties to 16, but the font is still 10 (is what it looks like).

Is this a known issue?

How to Automate Home Drive and MailBox Creation, within the creation of a new AD user?

$
0
0

Does anyone knows how to automate Home Drive Creation and Email account as well?

I have a server 2012R2 and 365 Exchange Server.

I know how to create Home Drive, then go to 365 to create a new email account on O365. 

Is there a way to create a script to do both as soon as i create a user I want my ad to create email account and map a home drive automatically ,,, Appreciate your help

Add Members to "Delivery Management" of the Distribution Group in Office 365.

$
0
0

Hi

we need to import File to add the list of members to the "Delivery Management" of the distribution groups. Is there any command or script is available.

Get-WmiObject : Invalid class "SMS_DPGroupContentDetails"

$
0
0

Hello, guys

I quering SCCM database via WMI-Object. Why its invalid class? I found in TechNet article https://technet.microsoft.com/en-us/library/dn581979.aspx

For example, if you wanted to convert the WMI class SMS_Advertisement to the associated SQL view, you would remove theSMS_ and replace it with v_, resulting in the appropriate view name ofv_Advertisement.

But my query isn't correct? Why>??

(Get-WmiObject -namespace $namespace -query "select PkgID  from SMS_DPGroupContentDetails" -ComputerName $SCCMServerName)

Get-WmiObject : Invalid class "v_SMS_DPGroupContentDetails"
At line:1 char:2
+ (Get-WmiObject -namespace $namespace -query "select PkgID  from v_SMS ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Disabling Network Adapter using WMI

$
0
0

Hello

I use the following line of code  to disable network adapters on laptops running windows 7 pro.

$nicsA =  gwmi win32_networkadapter |? {$_.configmanagererrorcode -eq '0'}
$nicsA | % {$_.disable()}

It works fine with powershell 3 but not with powershell 4.

I don't get any error, it just returns the following but adapter is still enabled.

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 5
PSComputerName   : 

Any idea ?

Viewing all 21975 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>