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

ISE additional spacing and temperamental colur issues w/ function

$
0
0

Hi All,

We've got some weird DNS issues where our DNS server doesn't seem to hold the latest addresses for our machines as provided by our DHCP server.

This is causing me a bit of a headache when trying to administer these machines remotely as I have to double check that the hostname points to the correct IP as otherwise I'll be making changes to a completely different machine than intended.

I thought a quick win would be a little PowerShell script. It seems to return the results as I'd expect, but there seem to be some inconsistencies with spacing when the script terminates, and issues with the colours that I've applied to some of the write-host outputs.

Weirdly, the colour output of the write-host doesn't appear to be an issue if I pass the Hostname parameter whilst calling the function. Only seems to be when I prompt the user by the Read-Host.

The idea is:

1) Get the IP of a given hostname

2) Get the hostname of that given IP (to check that this matches the original hostname)

3) Act accordingly with the results of the above

I've tweaked the code so you can run with 'test' as the hostname

Running w/ PowerShell 4.

Set-PSDebug    -Strict
Set-StrictMode -Version Latest

Clear-Host

Function Test-DNS
{
    [CmdletBinding()]
        Param 
        (
            # Allow the user to specify the FQDN if it hasn't already been supplied

            $Hostname =   (Read-Host "Please specify Hostname"),
            # Allow passing of the domain name. Corin FQDN by default.

            $Domain   =   'test.co.uk'
        )

    # Clear the screen after the read-host input

    Clear-Host

    # Throw an error if the hostname is not defined

    if(!($Hostname))
    {
        Write-Host        'You have not specified a hostname. This script will now terminate.'
        Write-Host        ''

        Break
    } 

    # Define variables and assign null values

    $IP_Response        = @()
    $IP_Count           = $null
    $Reverse_Lookup     = $null
    $Full_DNS_Name      = $null

    # Create the FQDN for a given hostname (if not already present)
    
    if($Hostname -like "*$Domain")
    {
        $Full_DNS_Name  = ($Hostname)
    }
    else
    {
        $Full_DNS_Name  = ($Hostname + '.' + $Domain)
    }

    # Display the hostname

    Write-Host            $Hostname
    Write-Host            ''

    # Try to grab the IP of a hostname and provide an error if this fails
    
    Try
    {
        $IP_Response   += '10.15.1.124' #([System.Net.Dns]::GetHostAddresses($Hostname) | Where {!($_.IsIPv6LinkLocal)}).IPAddressToString
        $IP_Count       = $IP_Response.Count
    }

    Catch [Exception]
    {
        Write-Host        'Error:' $_.Exception.Message -ForegroundColor Red
        Write-Host        ''
        
        Break
    }

    # Check the number of results returned and warn the user if there are more than one

    if($IP_Count -gt 1)
    {
         Write-Host       'WARNING:' $IP_Count 'addresses have been detected for this hostname.' -ForegroundColor Yellow
         Write-Host       ''
    }

    # Process each of the IPs that have been returned

    ForEach($IP in $IP_Response)
    {
        Try
            {
                $Reverse_Lookup = 'test' #([System.Net.Dns]::GetHostByAddress($IP).HostName)

                # If the reverse lookup matches the original hostname

                if(($Reverse_Lookup -eq $Hostname) -or ($Reverse_Lookup -eq $Full_DNS_Name))
                {
                    Write-Host    'Ping Response: ' $IP
                    Write-Host    'Reverse Lookup:' $Reverse_Lookup
                    Write-Host    ''
            
                    Write-Host    'Success: Reverse DNS lookup was successful.' -ForegroundColor Green
                    Write-Host    ''        
                }

                 # If the above is not true

                else
                {
                    Write-Host    'Ping Response: ' $IP
                    Write-Host    'Reverse Lookup:' $Reverse_Lookup
                    Write-Host    ''

                    Write-Host    'Error: Reverse DNS lookup did not match the provided hostname.' -ForegroundColor Red
                    Write-Host    ''
                }
            }
                 
        Catch [Exception]
            {
                Write-Host        $IP
                Write-Host        'Error:' $_.Exception.Message -ForegroundColor Red
                Write-Host        ''   
            }  
    }
}

Test-DNS

This is what happens after a few runs (usually when I've terminated the script part way through), without passing the Hostname parameter:

http://s16.postimg.org/sb2lehqdx/Output_1.png

(It also applies to some of the other write-host outputs), including error messages and the dbg messages that appear when I set breakpoints)

Now, onto the spacing issue...

If I continue to refresh the screen or rerun the script, it will sometimes randomly put two blank spaces before terminating. I've only specified one. I can't understand why this happens

This is how it appears when there are two spaces

http://s23.postimg.org/bgtg0hx5n/Output_3.png

I have just noticed that when it does display correctly (1 space), the first two characters of 'Success' are white, while the rest remain green.

Can anybody shed any light on this? It's driving me round the bend!

Cheers.


Enabling PowerShell Remoting SSL on Windows 7

$
0
0

Hello,

We have been asked to setup PowerShell Remoting HTTPS on our Windows 7 estate.  I was just wondering if there is a step-by-step guide (which even includes certificates creation etc.)?

HTTP option was rejected saying it's not secure. 

Any help would be highly appreciated.

Regards

[PowerShell] Disable File and Print Sharing on Public and Private Network Category

$
0
0

I am looking for the right powershell command for Windows 2012 so I can turn off File and Print Sharing (in the advanced sharing options) for the Private and Public network categories.  I wisht to keep it on for the Domain Category. 

I have not been able to identify either a registry key or a powershell cmdlet that will let me do this. 

Any assistance is appreciated.

problem with the powershell pipline parameter process

$
0
0

Hi,


I'm confused with the pipeline processing of object-property-parameter association.

Here my Cmdlet example

 

Function test-psboundbug{
	[CmdletBinding(DefaultParameterSetName='Parameter')]
 	param(
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param1,
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param2,
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param3,
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param4,
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param5
    )    
    Begin{}
    Process{
        Write-Host " ====== BEGIN - PROGRESS =============="
        $PSBoundParameters
        Write-Host " ====== BEGIN - PROGRESS =============="
    }
    End{}
}
$testParams=@()
$testParams+=@{Param1="Param1-1";Param2="Param2-1"}
$testParams+=@{Param1="Param1-2";Param2="Param2-2";Param3="Param3-2"}
$testParams+=@{Param1="Param1-3";Param2="Param2-3";Param4="Param4-3"}
$testParams+=@{Param1="Param1-4";Param2="Param2-4"}
$testParams+=@{Param1="Param1-5";Param2="Param2-5"}
$testParams|%{New-Object psobject -Property $_}|test-psboundbug

when i send a array of objects through a pipline, i thoucht in every loop in the ''PROCESS''-Scriptblock the parameter accociation from the object properties is caculate exact on the behavior of the single object, which is active in processing.
But when the Property-Names are alterate in pipeline (see the example above), powershell is not resetting the $PSBoundParameters variable,but rather the $PSBoundParameters contains old values from the parent object in the pipeline loop, up to the end.

Here the Output of the example above:

 ====== BEGIN - PROGRESS ==============

Key                                     Value 
---                                     -----
Param1                                  Param1-1
Param2                                  Param2-1
 ====== BEGIN - PROGRESS ==============
 ====== BEGIN - PROGRESS ==============
Param1                                  Param1-2
Param2                                  Param2-2
Param3                                  Param3-2
 ====== BEGIN - PROGRESS ==============
 ====== BEGIN - PROGRESS ==============
Param1                                  Param1-3
Param2                                  Param2-3
Param3                                  Param3-2
Param4                                  Param4-3
 ====== BEGIN - PROGRESS ==============
 ====== BEGIN - PROGRESS ==============
Param1                                  Param1-4
Param2                                  Param2-4
Param3                                  Param3-2
Param4                                  Param4-3
 ====== BEGIN - PROGRESS ==============
 ====== BEGIN - PROGRESS ==============
Param1                                  Param1-5
Param2                                  Param2-5
Param3                                  Param3-2
Param4                                  Param4-3
 ====== BEGIN - PROGRESS ==============

The $PSBoundParameters contains the value of Param3 und Param4 up to end of whole process. I can manually Reset $PSBoundParameter.Clean() on the End of the PROCESS-Block and all worked as expected.

My Question is , is this a bug or a feature.

Jens Müller


How to get OS version from Logon script

$
0
0

Hi ,

We are in need to create a logon script , where we should identify the OS version & proceed.

We have a mix of XP/Win7/Win2k8/Win2k8R2/Win2k3/Win2k12)

1) We have tried using "Systeminfo" but this takes 10 secs of time to run.

2) We have tried ver , but ver will give same ver number for Win7 & Wink8R2.

3) Using wmi , wmi might not be installed on XP PCs.

Can any one please suggest a way!!

Appreciate your kind help - Thanks :)


Sai Krishna

Error loading Virtual Machine Manager 2012 and Data Protection Manager 2012 modules together

$
0
0

Hi.

Very strange issue: when i try to load together Virtual Machine Manager 2012 and Data Protection Manager 2012 modules - script fails. If first loaded Virtual Machine Manager 2012 module:

Import-Module : Cannot load Windows PowerShell snap-in C:\Program Files\Microsoft Data Protection Manager\DPM2012\bin\ObjectModelCmdlet.dll because of the following error: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Loader Exceptions:
Could not load type 'Microsoft.Internal.EnterpriseStorage.Dls.Utils.AlertTypes.AlertEnum' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
At C:\Program Files\Microsoft Data Protection Manager\DPM2012\bin\Modules\DataProtectionManager\DataProtectionManager.psm1:2 char:14
+ Import-Module <<<<  (Join-Path $cmdletDllPath ObjectModelCmdlet.dll)
    + CategoryInfo          : ResourceUnavailable: (:) [Import-Module], PSSnapInException
    + FullyQualifiedErrorId : PSSnapInLoadFailure,Microsoft.PowerShell.Commands.ImportModuleCommand

If first loaded DPM 2012 module::

Import-Module : Cannot load Windows PowerShell snap-in C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\Microsoft.SystemCenter.VirtualMachineManager.dll because of the following error: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Loader Exceptions:
Could not load type 'Microsoft.VirtualManager.Utils.TypeConverterEnumHelper`1' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.TypeConverterEnumHelper`1' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineException' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Method 'get_BootOrder' in type 'Microsoft.SystemCenter.VirtualMachineManager.ClientObjectHWSettingsSourceAdapter' from assembly 'Microsoft.SystemCenter.VirtualMachineManager, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMStartAction' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Method 'get_BootOrder' in type 'HardwareConfigSettingsAdapter' from assembly 'Microsoft.SystemCenter.VirtualMachineManager, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
Could not load type 'Microsoft.VirtualManager.Utils.VMStartAction' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMStartAction' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMStartAction' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMSCSIControllerType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMSCSIControllerType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.ServicingTypeValues' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.FabricCapabilityType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.SKUType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.VMComputerSystemState' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Could not load type 'Microsoft.VirtualManager.Utils.CarmineObjectType' from assembly 'Utils, Version=1.0.523.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
At \\team\dfs\scripts\Admin\Scvmm\Move-VirtualMachine.ps1:103 char:16
+         Import-Module <<<<  'C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\psModules\virt
ualmachinemanager\virtualmachinemanager.psd1';
    + CategoryInfo          : ResourceUnavailable: (:) [Import-Module], PSSnapInException
    + FullyQualifiedErrorId : PSSnapInLoadFailure,Microsoft.PowerShell.Commands.ImportModuleCommand

search multiple folders for files with same name and create single file

$
0
0

 I have a project where I need to search multiple folders for a file name and when found append data from each file to a single input file.

 Example

root folder to start search
\\servera\sales

\\servera\it\salesa\cmmstr.txt
\\servera\it\salesb\cmmstr.txt
\\servera\it\salesc\cmmstr.txt

 I need to create a a single cmmstr.txt on the root folder. I would like it to be able to run this with parms to pass in folders to search and file names to search and single file name to create. I'm going to have a least 10 differnt files to search for and create output file for. The folders to search
will somewhat be static.

 Thanks.

 

Make a new child domain(Domain2) using Powershell

$
0
0

I am trying to make a new child domain(Domain2) using Powershell.

$user = Get-ADUser 'CN=post master,CN=Users,Domain2,DC=Domain1,DC=com' -Server "Domain2.Domain1.com"
Add-ADGroupMember "Test Users" -Members $user



How to get the DNS Domain Lease of the DHCP Server ?!

$
0
0

Hi guys,

first of all my apologies.. I am a newbie at powershell...

I need to get the DNS Domain Lease given by the DHCP Server via Powershell... does anyone has

realized this before?! Google did not point me to the correct answers so far...

I've seen the following Get Command which displays me a lot of the IP Configuration but how do I get a simple string with only the specific DNS server (e.g. uk.abc.com) from it? -> 

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*

Thanks in advance,

Markus


Get-ADUser and Get-Mailbox

$
0
0

Environment: AD running on Windows 2008R2 and Email on Office 365 (cloud only - not on premise)

I am trying to initially find all mailboxes where LitigationHoldEnabled is set to true in a particular OU and then set those mailboxes to false but I'm running into different errors depending on how I write the commands.  I know this is a syntax error because of my limited knowledge of PowerShell so I'm hoping someone can help.

Scenario 1 with error (I did connect to online session before running these commands):

$quarantineOU = get-mailbox -OrganizationalUnit "OU=Quarantine,DC=domain,DC=corp"

error:

Couldn't find organizational unit "Quarantine". Make sure you have typed the name correctly.

    + CategoryInfo          : NotSpecified: (:) [], ManagementObjectNotFoundException

    + FullyQualifiedErrorId : [Server=BLUPR06MB195,RequestId=89e992ba-ad64-4392-887a-626217e46278,TimeStamp=5/14/2014 4:17:41 PM] [FailureCate

   gory=Cmdlet-ManagementObjectNotFoundException] BD65057B

    + PSComputerName        : pod51043psh.outlook.com

Scenario 2 with error:

$qu = get-aduser -filter * -searchbase "OU=Quarantine, DC=domain, DC=corp"
$qu | ForEach-Object (Get-Mailbox $_.userprincipalname -ResultSize unlimited | where {$_.LitigationHoldEnabled -eq $true})

Error:

ForEach-Object : Parameter set cannot be resolved using the specified named parameters.

At line:2 char:7

+ $qu | ForEach-Object (Get-Mailbox $_.userprincipalname -ResultSize unlimited | w ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : MetadataError: (:) [ForEach-Object], ParameterBindingException

    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ForEachObjectCommand

Open a URL 5 different times a day based on times from an EXCEL spreadsheet.

$
0
0
HI there

I would like to open a specific URL at 5 different times of the day, everyday for the whole year using windows powershell. These times would change everyday by a minute or so and therefore i would have all the times for each day of the year specified in a Excel spreadsheet.

The URL should open in IE and should be open for about time minutes and then close.

I have the code to open the url but how would i get it to open on the exact time based on the time in my excel spreadsheet.

Heres the code to open the URL:

$IE=new-object -com internetexplorer.application
$IE.navigate2("http://www.vocfm.ndstream.net/wmplayer.htm")
$IE.visible=$true


In the spreadsheet i have 365 rows from 1 jan to 31 dec with columns date, time1, time2, time3, time4, time5.



IF i could use something like 

If Get-Date = 1 jan

then go to times.xlsx where row = 1 jan and check if time1 = current time and then run the URL .ps1 mentioned above.

Thanks in advance for any help. 


Drive Optimization -- Setting the frequency

$
0
0

Ok,

I have been searching for a way to set the scheduled optimization of a drive to Daily, vs the default weekly.

I would like to do this via powershell, so ican ensure that all my Servers are standardized in this fashion.

I have been looking at the command Optimize-Volume...and have found ways to analyze the drive, and to optimize the drive vial powershell, but have not been able to find a way to change the scheduled Frequency.

any ideas?

-Guy

need help creating a powershell script to call a list of networked servers and to find a file in each

$
0
0

Hi Experts, plesae help creating a powershell script to call a list of networked servers and to find a file in each. This should search in all servers irrespective of model, architecture and OS version and in elevated mode and should be able to tackle UAC settings. :)


Regards, Vivek Nambiar

Powershell & C# to check if host available

$
0
0

Hi All,

I have a c# code segment which process a power-shell script. The following is the code segment.

Runspace objRunspace = null;
objRunspace = RunspaceFactory.CreateRunspace();
objRunspace.Open();
PowerShell powershell = PowerShell.Create();
powershell.Runspace = objRunspace;
powershell.AddScript("my powershell script");
Collection<PSObject> objPS1 = powershell.Invoke();
if (objPS1.Count == 0)
   {
	//I Assumes that the PC is down
   }

In this case, If the login credentials are wrong or if that particular PC is not available; the ObjPS1.count will be zero. If that count is Zero, I assumes that the host is unavailable. But in accuracy point of view, I want to identify both these (wrong credentials / host unavailable) situations separate. Is there a better way or different approach to achieve the same.

Thanks in advance
Sebastian

Get-WinEvent remote time

$
0
0
When using get-winevent to return events from a remote machine, the time is adjusted to local time.  Is there any way that I can see the event time as the remote machine's time?  WinRM is not enabled.

Making my script faster.

$
0
0

So i've made a script that checks a CSV file for IP configuration settings and sets them on the list of corresponding machines.

the csv data fields are like this.

Machine : machine1.contoso.com
IP      : 10.1.91.101
Subnet  : 255.255.255.0
Gateway : 10.1.91.1
DNS     : 10.1.93.50
DNS2    : 10.1.22.50


my script is

$config = import-csv -path .\config.csv

$ErrorActionPreference = "SilentlyContinue"

foreach ($vm in $config.machine){

if($vm.length -ge 1){

$settings = Get-WmiObject win32_networkadapterconfiguration -computername $vm -filter "ipenabled = 'true'"
$dnsrecord = ($config.dns , $config.dns2)
$settings.setdnsserversearchorder($DNSrecord)
$settings.setgateways($config.gateway)
$settings.enablestatic($config.ip , $config.subnet)
echo $vm "complete"
}

}

This script works except for 2 issues.

The first is sometimes(randomly) it skips the setgateways command

The second is that it takes forever to finish.

any recommendations?

Powershell to delete listed folders from a text file

$
0
0

Hi,

I am trying to come up with a Powershell script to query a text file with a list of folders, delete the folders listed and create a log file of the deleted folders.

I am new to this, so do bare with me, this is what i have come up with so far. But it does not seems to do anything. Any ideas would be appreciated.

Thanks in advance.

*******************************************************************************************************************************

$Deleted_Users = "C:\Test\Delete\filelist.txt" 
$Home_Folders = "C:\Test\Delete"

$UserList = Import-Txt $filelist

$UserList | ForEach-Object {    
   $ID = $_.ID  
   $User_Home = $Home_Folders + "\" + $_.ID }

Remove-Item $Home_Folders | Out-File c:\Test\Delete\Deleted.txt }

*******************************************************************************************************************************

Powershell Create Objects

$
0
0

Hi,

I'm trying to create objects in powershell.  I know this can be done through hash but I'd like to practice with objects.

Any help is appreciated.

In Excel they are entered as:

ITEM1
                CM   PM
Apr2014    17    18
May2014   20    22

ITEM2
                CM  PM
Apr2014    19  12
May2014   14  28

Thank you

powershell script to search specific event ID's from Archive Event files

Error: "File cannot be loaded because the execution of scripts is disabled on this system"

$
0
0

Hello,

I'm executing powershell scripts from command prompt. I've changed my execution policy to Unrestricted. Also, Get-ExecutionPolicy is showing 'Unrestricted'. When i execute following error

"File cannot be loaded because the execution of scripts is disabled on this system"

Reason ?


Sohaib Khan
Viewing all 21975 articles
Browse latest View live


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