El TecnoBaúl de Kiquenet

Kiquenet boring stories

Posts Tagged ‘powershell’

Arrays in Powershell

Posted by kiquenet en 9 enero 2015

Collections, Hashtables, Arrays, strings
https://www.simple-talk.com/sysadmin/powershell/powershell-one-liners–collections,-hashtables,-arrays-and-strings/

Define Array

$configs = @(

     "C:\tfs\Arquitectura\ConnectionStrings.config"; 
     "C:\tfs\VSIntegration\Test\App.config"; 
)

Foreach Array

for( $i = 0; $i -lt $configs.length; $i++) {
  $config = $configs[$i]
 
  Write-Host setting $config
 
}

Alternative

function ConvertTo-MyTypeOfItem
{
    PARAM (
        [ValidatePattern("([^_]+_){3}[^_]+")]
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$StringToParse
    )

    PROCESS {
        $custId, $invId, $prodId, [int]$value = $StringToParse -split "_"
        $myObject = New-Object PSObject -Property @{
            CustomerID = $custId;
            InvoiceID = $invId;
            ProductID = $prodId;
            Value = $value
        }
        Write-Output $myObject
    }
}

# In the test scenario I have replaced getting the list of files
# with an array of names. Just uncomment the first and second lines
# following this comment and remove the other $baseNames setter, to
# get the $baseNames from the file listing

#$files = Get-ChildItem test *.txt
#$baseNames = $files.BaseName
$baseNames = @(
    "cust1_inv1_prod1_1";
    "cust2_inv2_prod2_2";
    "cust3_inv3_prod3_3";
    "cust4_inv4_prod4_4";
)

$myObjectArray = $baseNames | ConvertTo-MyTypeOfItem

$myObjectArray

Sources:
http://stackoverflow.com/questions/21884828/powershell-array-to-store-strings
http://stackoverflow.com/questions/25191803/powershell-cli-foreach-loop-with-multiple-arrays
http://www.powershellpro.com/powershell-tutorial-introduction/variables-arrays-hashes/

Anuncios

Posted in PowerShell, Scripts | Etiquetado: , | Leave a Comment »

Members, Contributor, Groups, Identities in TFS

Posted by kiquenet en 5 enero 2015

Get members of TFS Group

private List<Identity> ListContributors()
{
    const string projectName = "<<TFS PROJECT NAME>>";
    const string groupName = "Contributors";
    const string projectUri = "<<TFS PROJECT COLLECTION OR URL TFS SERVER>>";

    TfsTeamProjectCollection projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(projectUri));
    ICommonStructureService css = (ICommonStructureService) projectCollection.GetService(typeof(ICommonStructureService));
    IGroupSecurityService gss = projectCollection.GetService<IGroupSecurityService>();

    // get the tfs project
    var projectList = css.ListAllProjects();
    var project = projectList.FirstOrDefault(o => o.Name.Contains(projectName));

    // project doesn’t exist
    if (project == null) return null;

    // get the tfs group
    var groupList = gss.ListApplicationGroups(project.Uri);
    var group = groupList.FirstOrDefault(o => o.DisplayName.Contains(groupName));  // you can also use DisplayName or AccountName. AccountName is empty for me.

    // group doesn’t exist
    if (group == null) return null;

    Identity sids = gss.ReadIdentity(SearchFactor.Sid, group.Sid, QueryMembership.Expanded);

    // there are no users
    if (sids.Members.Length == 0) return null;

    // convert to a list
    List<Identity> contributors = gss.ReadIdentities(SearchFactor.Sid, sids.Members, QueryMembership.Expanded).ToList();

    return contributors;
}

Add SID to a Readers group

# load the required dll
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")

function get-tfs
{
    param(
    [string] $serverName = $(throw ‘serverName is required’)
    )

    $propertiesToAdd = (
        (‘VCS’, ‘Microsoft.TeamFoundation.VersionControl.Client’, ‘Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer’),
        (‘WIT’, ‘Microsoft.TeamFoundation.WorkItemTracking.Client’, ‘Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore’),
        (‘CSS’, ‘Microsoft.TeamFoundation’, ‘Microsoft.TeamFoundation.Server.ICommonStructureService’),
        (‘GSS’, ‘Microsoft.TeamFoundation’, ‘Microsoft.TeamFoundation.Server.IGroupSecurityService’)
    )

    [psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
    foreach ($entry in $propertiesToAdd) {
        $scriptBlock = ‘
            [System.Reflection.Assembly]::LoadWithPartialName("{0}") > $null
            $this.GetService([{1}])
        ‘ -f $entry[1],$entry[2]
        $tfs | add-member scriptproperty $entry[0] $ExecutionContext.InvokeCommand.NewScriptBlock($scriptBlock)
    }
    return $tfs
}
#set the TFS server url
[psobject] $tfs = get-tfs -serverName
http://YourTfsServer:8080/tfs/YourColleciton

$items = $tfs.vcs.GetAllTeamProjects( ‘True’ )
    $items | foreach-object -process {
    $proj = $_
    $readers = $tfs.GSS.ListApplicationGroups($proj.Name) | ?{$_.DisplayName -eq ‘Readers’ }

    $tfs.GSS.AddMemberToApplicationGroup($readers.Sid, ‘TheSidToTheGroupYouWantToAdd’)
}

var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://vsalm:8080/tfs/FabrikamFiberCollection"));

            var ims = tpc.GetService<IIdentityManagementService>();

            var tfsGroupIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
                                                    "[FabrikamFiber]\\Fabrikam Fiber Web Team",
                                                    MembershipQuery.None,
                                                    ReadIdentityOptions.IncludeReadFromSource);           

            var userIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
                                                    "VSALM\\Barry",
                                                    MembershipQuery.None,
                                                    ReadIdentityOptions.IncludeReadFromSource);

            ims.AddMemberToApplicationGroup(tfsGroupIdentity.Descriptor, userIdentity.Descriptor);

Sources:
http://stackoverflow.com/questions/16792995/get-members-of-tfs-group

http://stackoverflow.com/questions/7961727/how-to-grant-read-only-access-to-all-tfs-team-projects-to-a-group-of-users/7971731#7971731

http://blog.ronischuetz.com/2010/04/tfs-api-list-all-tfs-users.html

http://pauravlokesh.wordpress.com/2013/08/13/recursively-list-groups-users-tfsteamprojectcollection/

http://geekswithblogs.net/TarunArora/archive/2011/09/30/tfs-sdk-get-groups-users-permissions-using-tfs-api-with.aspx

Posted in .NET, PowerShell, Scripts, TFS | Etiquetado: , , | Leave a Comment »

Delete TestResults folder (Unit Testing Visual Studio)

Posted by kiquenet en 5 enero 2015

Cleaning up Visual Studio TestResults with PowerShell

If you write your unit tests using Visual Studio, then you know how quickly those pesky "TestResults" folders can eat up precious disk space over time.

Assuming all of your code is collocated in the same parent directory, replace "C:\TFS" with your parent directory.

(get-childitem C:\TFS\* -recurse) | where{$_.name -eq "TestResults"} |% {remove-item $_.fullname -recurse}

Improvements:

Error Handling when deleting folders and files.

Validating contents TestResults folder (subfolders In and Out, *.trx files, …)

$dir = "C:\TFS\"
(get-childitem $dir -recurse) | where { $_.name -eq "TestResults"} | foreach {

   $delete = $false;
   (get-childitem  $_.FullName -recurse) | where {$_.name -eq "Out"}  | foreach {
       
        $ParentS = ($_.Fullname).split("\")
        $Parent = $ParentS[@($ParentS.Length – 3)]
        if ($Parent -eq "TestResults") {$delete = $true;}

   }

   if ($delete)
   {
        Write-Host -ForegroundColor red ("Eliminando {0}" -f  $_.FullName )
        Remove-item $_.fullname -recurse
   }

}

Write-Host -ForegroundColor red ("Eliminado TestResults de {0}" -f  $dir )

Source:
http://captechconsulting.com/blog/mike-etheridge/cleaning-visual-studio-testresults-powershell

Posted in .NET, PowerShell, Productividad, Scripts, VisualStudio | Etiquetado: , , | Leave a Comment »

Web Administration (IIS) , AppPools, WebSites using PowerShell

Posted by kiquenet en 6 agosto 2014

Trabajando con Powershell (Powershell Remoting) y AppPools – WebSites

Enter-PSSession –Computername SERVER-Credential SERVER\instalador

      (Nota:en este punto os pedirá la contraseña correspondiente al usuario)

Import-Module WebAdministration

Get-ChildItem IIS:\AppPools

Get-ChildItem IIS:\AppPools | ? {$_.state -eq "Started" -and $_.name –eq "myAppPool"}

Get-ChildItem -Path IIS:\Sites

Get-ChildItem -Path IIS:\Sites | ? {$_.state -eq "Started"}

Get-ChildItem -Path "IIS:\Sites\Default Web Site"

Get-ChildItem -Path "IIS:\Sites\Default Web Site" | ? {$_.name -eq "MyWebsite"}

Exit-PSSession

References:

http://technet.microsoft.com/es-es/library/ee909471(v=ws.10).aspx
http://www.iis.net/learn/manage/powershell/powershell-snap-in-creating-web-sites-web-applications-virtual-directories-and-application-pools
http://stackoverflow.com/questions/6616320/stop-default-web-site-by-powershell

Posted in PowerShell, Scripts | Etiquetado: , , | Leave a Comment »

Powershell Version–What Version is installed

Posted by kiquenet en 29 julio 2014

Use $PSVersionTable.PSVersion to determine the engine version. If the variable does not exist, it is safe to assume the engine is version 1.0.

Note that $Host.Version and (Get-Host).Version are not reliable – they reflect the version of the host only, not the engine. PowerGUI, PowerShellPLUS, etc. are all hosting applications, and they will set the host’s version to reflect their product version, which is entirely correct.

PS C:\> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1

$PSVersionTable is more reliable and returns $PSVersion. You can also use $PSVersionTable.PSVersion. Even if you are connected remotely to the machine running different version (invoke-command -computername myRemotePC -Credential foo {$host}), it looks like $host will just show the lowest version they agreed upon for serializing. While $PSVersionTable will show the true version

About Host vs $PSVersionTable

Get-Host or $PSVersionTable. As Andy Schneider points out, $PSVersionTable doesn’t work in version 1; it was introduced in version 2.
$host.version is just plain wrong/unreliable. This gives you the version of the hosting executable (powershell.exe, powergui.exe, powershell_ise.exe, powershellplus.exe etc) and not the version of the engine itself.

The engine version is contained in $psversiontable.psversion. For PowerShell 1.0, this variable does not exist, so obviously if this variable is not available it is entirely safe to assume the engine is 1.0, obviously.

 

Microsoft recommends checking the registry to see which version, if any, is installed
https://blogs.msdn.com/b/powershell/archive/2009/06/25/detection-logic-poweshell-installation.aspx?Redirected=true

  • To check if any version of PowerShell is installed, check for the following value in the registry:

    • Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1
    • Value Name: Install
    • Value Type: REG_DWORD
    • Value Data: 0x00000001 (1
  • To check whether version 1.0 or 2.0 of PowerShell is installed, check for the following value in the registry:
    • Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine
    • Value Name: PowerShellVersion
    • Value Type: REG_SZ
    • Value Data: <1.0 | 2.0>

Remember: 

  • Depending on any other registry key(s), or version of PowerShell.exe or the location of PowerShell.exe is not guaranteed to work in the long term.

  • PowerShell 2.0 doesn’t support side by side installations with 1.0, but 2.0 is back-wards compatible with 1.0.

To determine if PowerShell is installed, you can check the registry for the existence of HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1\Install and, if it exists, whether the value is 1 (for installed), as detailed in the blog post Check if PowerShell installed and version.

To determine the version of PowerShell that is installed, you can check the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\PowerShellVersion.

To determine the version of PowerShell that is installed from a .ps1 script, you can use the following one-liner, as detailed on PowerShell.com in Which PowerShell Version Am I Running.

For PowerScript 3.0 there seems to be a new entry,
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3 (in addition toHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1).


The proper place in this case for getting the version appears to be
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine\PowerShellV‌​ersion(sample value is "3.0").




$isV2 = test-path variable:\psversiontable

The same site also gives a function to return the version:

function Get-PSVersion {
    if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
}

Test Powershell Version.cmd

@echo off
echo Checking powershell version...
del "%temp%\PSVers.txt" 2>nul
powershell -command "[string]$PSVersionTable.PSVersion.Major +'.'+ [string]$PSVersionTable.PSVersion.Minor | Out-File ([string](cat env:\temp) + '\PSVers.txt')" 2>nul
if errorlevel 1 (
 echo Powershell is not installed. Please install it from download.Microsoft.com; thanks.
) else (
 echo You have installed Powershell version:
 type "%temp%\PSVers.txt"
 del "%temp%\PSVers.txt" 2>nul
)
timeout 15

References:
http://stackoverflow.com/questions/1825585/how-to-determine-what-version-of-powershell-is-installed

Posted in PowerShell, Scripts | Etiquetado: , | Leave a Comment »

Using PowerShell to Manage Network Interfaces and Windows Services

Posted by kiquenet en 28 julio 2014

PowerShell is a new scripting language that allows you to interact with applications, services and objects as objects. It is a .NET application which shells commands out on your behalf. This is very powerful, because it allows you to leverage the strengths of an object oriented model within your scripting tasks with a very terse, yet simple scripting language. Because it is written in .NET, you can access .NET types, objects and WMI objects from a command prompt.

My current workstation runs Windows Server 2008 Standard Edition with Hyper-V. I use the host/parent partition as my office, and have a number of child partitions that are integrated into a Windows Server 2008 Domain. Needless to say, this configuration gives me everything I need to develop, test and integrate multiple Server OS’, platforms and technologies, and is my environment of choice for developing production code. It still amazes me how far we’ve come in such a short period of time. Not too long ago, I remember setting up similar lab environments at home running Windows NT 4 and Windows Server 2000 on dedicated physical machines.

Anyway, I have to say that I am absolutely delighted with my environment, but it took me some time and effort to learn how to tune Server 2008, and this is all well documented in previous posts. One of the the things that I do to keep performance high is that I only have the 3 services required for Hyper-V running when I need them. In other words, if I am only working with email and writing documents or design drawings, I don’t need to have the Hyper-V services running because they are resource intensive. In addition, I only want my loopback adapter (that provides me with a virtual LAN for all of the child partitions on my domain to communicate) enabled when I need it to be. One reason for this, is that if I leave my loopback on and reboot, boot times can take up to 10 minutes because the loopback is configured to use my domain controller as the primary DNS server, which is a VM. Because the DNS Server only runs when my Domain Controller VM  is up and running, the VM will only come up when I start it. As a result, Windows tries and tries to reach the DNS server for the Loopback adapter until it finally gives up (If anyone knows how to change this timeout, please shoot me a note or post a comment).

So, I only want Hyper-V services running when I need to work in my development environment, and I don’t want my loopback enabled unless it needs to be. For a while, I was managing this as follows…

First, I set the following services to "Manual", so that they do not start automatically:

  • vmms: Hyper-V Virtual Machine Management
  • vhdsvc: Hyper-V Image Management Service
  • nvspwmi: Hyper-V Networking Management Service

The service names are a bit obscure, but the "friendly" names are pretty self explanatory. When I need to start my development environment, I would go into Server Manager, and start each service one by one.

Next, I would enable my loopback adapter so that my child partitions can communicate with each other.

All together, this resulted in a number of clicks, which was somewhat mundane to do every time. Worse, once I enabled the loopback, I often would forget to disable it before shutting down (remember, servers running Hyper-V do not support hibernation). For a while, I thought about writing myself a sticky note and posting it to my forehead so that I would not forget to disable the loopback, and finally, I decided to create an easy button for starting and stopping my development environment.

This is where Powershell comes in.

Using PowerShell to Query and Manage Network Adapters

The first thing I did was figure out how to talk to my network adapters so that I could enable and disable my loopback as needed. It turns out that Microsoft provides a WMI object called Win32_NetworkAdapter. The object exposes a bunch of properties for working with an instance of a Win32_NetworkAdapter:

image

In addition, there are 4 public methods that are exposed, and well documented as shown below:

image

To enumerate all network adapters on my host/parent partition, I issued the following command:

   1: Get-wmiobject win32_NetworkAdapter | format-table 

PowerShell lists each adapter and certain properties in a tabular format because I added a pipe and format-table parameter:

image

To identify the instance that corresponds to my loopback, I simply need to find the "Internal VLAN" instance above, which has a DeviceID of 17:

image

Next, I want to retrieve an instance of the adapter with a DeviceId of 17, so I issue the following statement:

   1: Get-WmiObject win32_networkadapter | where {$_.DeviceId -eq 17}

The where keyword uses a bracketed expression to evaluate the condition. The "$" is a temporary variable, which is similar to the "this" keyword in C#, which provides context for the current instance. The "-eq" operator is the equality operator in PowerShell. So, we are querying all adapters for the adapter with a DeviceID equal to 17. The above command returns the following:

image

Notice that each property is an actual documented property of the WIN32_NetworkAdapter object. If we have access to properties, it would be helpful to determine if the adapter is enabled or disabled. To do this, I assign the adapter to a variable called $adapter as shown below, and then I get the value of the Availability property:

image

The value of the Availability property is 3. Referring to the WIN32_NetworkAdapter documentation, I can see that a status of 3 indicates that adapter is on, and running on full power.

image

Are you getting the hang of it yet? Hopefully, the interaction with PowerShell should feel object-oriented because it is! We are getting a reference to the WMI shell of the adapter and then using it’s get accessors to get the value of the public properties. So, if we can get a reference to the adapter, get properties, we should be able to call methods on it, right?

To disable the adapter, I simply call the Disable() method on my $adapter variable:

image

Now, to confirm that the adapter is disabled, you can look at Network Connections and you will see that the Status is in fact Disabled. To do this programmatically via PowerShell, you can use the ConfigManagerErrorCode property which is also documented. Get a new instance of the adapter, and call the ConfigManagerErrorCode property on it as shown below:

image

A return code of 22 can be matched to the table in the MSDN documentation:

image

Now, enable the adapter by calling the Enable() method:

image

A return code of 0 indicates that the adapter is enabled, and working properly:

image

As you can see, PowerShell is a very easy to use, yet powerful tool for managing system objects in an object-oriented manner. You don’t need to worry about writing VBScript or C# to accomplish simple administrative tasks such as enabling and disabling a network adapter. As you might imagine, the real power comes in being able to run a series of PowerShell commands in a batch, perhaps at the click of the button. This is exactly what I’ll cover next.

Using PowerShell to Query and Manage Windows Services

You’ll recall that I only want the 3 Hyper-V services to run when I need them, so what I want to do is create an "easy button" to toeggle my development environment on and off.

The "On" should:

  • Enable my Internal VLAN Adapter
  • Start the Hyper-V Virtual Machine Management
  • Start the Hyper-V Image Management Service
  • Start the Hyper-V Networking Management Service
  • Get me a cup of Starbucks coffee

The "Off" Button should:

  • Disable my Internal VLAN Adapter
  • Disable the Hyper-V Virtual Machine Management
  • Disable the Hyper-V Image Management Service
  • Disable the Hyper-V Networking Management Service


    I’ve already covered how to manage network adapters using PowerShell as a primer, so I’ll jump right into working with Windows Services. As with any program, sometimes it is helpful to group commands into a subroutine. In PowerShell, these functions are referred to as cmdlets (prounounced "commandlets"). You may not realize this, but you’ve already been working with cmdlets if you’ve tried the commands I covered above on your own machine. The get-wmiobject command is actually a cmdlet that provides a reference to the WMI object specified as the parameter. You could accomplish this without the cmdlet, but why would you want to?

  • Fortunately, a cmdlet is also available for working with Windows Services: get-service.

    To get information about a particular service, simply call get-service with the service name. The actual service name of the Hyper-V Virtual Machine Management service is "vmms", so issuing the get-service vmms command returns a few properties including the Name, DisplayName and Status of the service.

    image

    Starting and stopping the Hyper-V Virtual Machine Management service is as simple as calling the appropriate method: Stop() to stop the service and Start() to start it. While you could use a variable called $service to store the reference to the service object, an abbreviated way to accomplish this is shown below:

       1: (get-service vmms).Stop()

    Now, when you issue the get-service vmms command, you can see that the Status property is "Stopped":

    image

    Starting the service back up is as simple as calling Start() on the cmdlet expression:

    image

    Building the "Easy Buttons"

    There may very well be a more sophisticated way to do this, but I created two batch files, one called "Start.bat" and the other called "Stop.bat" and placed them in my documents folder. Each file contains the appropriate PowerShell syntax as shown below:

    image

    Note, to re-use variables across PowerShell sessions, you must first configure a PowerShell profile: http://msdn.microsoft.com/en-us/library/bb613488(VS.85).aspx

    Next I created two desktop shortcuts, appropriately named "Start" and "Stop" and set the target to each corresponding file.

    Now, I have two "Easy Buttons" for toggling my development environment on and off. While I can’t guarantee that I won’t forget to press the "off  button" before shutting down, it is a heck of a lot easier than going through the contortions manually.


  • References:

    http://rickgaribay.net/archive/2008/09/26/using-powershell-to-manage-network-interfaces-and-windows-services.aspx

    Posted in PowerShell, Scripts, Soporte | Etiquetado: , , | Leave a Comment »

    Powershell–Troubleshooting Group Policy – Privileges

    Posted by kiquenet en 28 julio 2014

    Useful when troubleshooting things like group policy, scripts or any other sources that may change privileges from what they should be. May generate a hash from this and automate a quick hash difference to monitor changes to privileges on systems.

    Command
    "secedit.exe", "/export /areas USER_RIGHTS /cfg $file"

    #Variables
    $computername= $env:COMPUTERNAME
    $tempdir = "c:\temp"
    $file = "$tempdir\privs.txt"
    [string] $readableNames
    $outHash = @{}

    ##### Generate report with just USER_RIGHTS ######
    $process = [diagnostics.process]::Start("secedit.exe", "/export /areas USER_RIGHTS /cfg $file")
    $process.WaitForExit()

    $in = get-content $file
    # start check of local userrights
    foreach ($line in $in) {
    if ($line.StartsWith("Se")) {
    $privilege = $line.substring(0,$line.IndexOf("=") – 1)
    $sids = $line.substring($line.IndexOf("=") + 1,$line.Length – ($line.IndexOf("=") + 1))
    $sids =  $sids.Trim() -split ","
    $readableNames = ""
    foreach ($str in $sids){
        Write-Host $str
        if ($str.StartsWith("*S-")) {
            $str = $str.substring(1) # For some reason, secedit appends a * to the SID like *S-1-5-32-544, this just strips the first character
            $sid = new-object System.Security.Principal.SecurityIdentifier($str)
            $readableName = $sid.Translate([System.Security.Principal.NTAccount])
            $readableNames = $readableNames + $readableName.Value + ","
        } else {
            $readableName = $str
            $readableNames = $readableNames + $readableName + ","
        }
      }
    $outHash.Add($privilege,$readableNames.substring(0,($readableNames.Length – 1))) #output edited version
    }
    }
    $outHash

    References:
    http://rcampi.blogspot.com.es/2011/08/powershell-ghetto-way-to-output.html

    Posted in PowerShell, Scripts | Etiquetado: , , | Leave a Comment »

    Sharepoint, Powershell, BDC Model

    Posted by kiquenet en 28 julio 2014

     

    SharePoint: Removing a BDC model via PowerShell

       In one of the previous posts I showed how to import a BDC model; the current one is about removing. The following script allows to remove a bdc model by its name:

    $model = Get-SPBusinessDataCatalogMetadataObject

    -Name "yourModelName" -BDCObjectType Model -ServiceContext "http://yourWebAppUrl"

    Remove-SPBusinessDataCatalogModel -identity $model -Confirm:$false

    Note: here command line parameters are wrapped to the next lines for readability only. In SharePoint 2010 Management Shell, each command and its parameters should be in the same line.

    The Get-SPBusinessDataCatalogMetadataObject command gets a Model object by its name and saves reference to it in the $model variable. The Model object is relevant to your web application accessible through the url http://yourWebAppUrl. As its name implies, the Remove-SPBusinessDataCatalogModelmethod removes the received model using the $model variable. -Confirm:$false allows to skip an YES/NO confirmation arising right before a model is deleted indeed. Remember that the operation isn’t reversible, use the -Confirm:$false with precaution.

    If you need to remove all bdc models, you can use the script as follows:

    $metaStore = Get-SPBusinessDataCatalogMetadataObject

    -BdcObjectType Catalog -ServiceContext "http://yourWebAppUrl"

    foreach ($model in $metaStore.GetModels("*")) {

    Remove-SPBusinessDataCatalogModel –Identity $model -Confirm:$false

    }

    Note: here command line parameters are wrapped to the next lines for readability only. In SharePoint 2010 Management Shell, each command and its parameters should be in the same line.

     

    SharePoint: Deploying a BDC model via PowerShell

        In SharePoint 2010 a BDC model can be easily deployed using PowerShell. Launch SharePoint 2010 Management Shell (click on Start, then All Programs -> Microsoft SharePoint 2010 Products ->SharePoint 2010 Management Shell) and execute the following script:

     

    $metaStore = Get-SPBusinessDataCatalogMetadataObject

    -BdcObjectType "Catalog" -ServiceContext "http://yourWebAppUrl"

    Import-SPBusinessDataCatalogModel

    -Path "c:\folder\subfolder\yourfile.bdcm" -Identity $metaStore -Force

    Note: here command line parameters are wrapped to the next lines for readability only. In SharePoint 2010 Management Shell, each command and its parameters should be in the same line.

    The first command, Get-SPBusinessDataCatalogMetadataObject, returns and saves in the $metaStorevariable a Catalog metadata object relevant to your web application accessible through the url http://yourWebAppUrl. The second command, Import-SPBusinessDataCatalogModel, imports a Business Data Connectivity Model defined in a file, path of which is indicated by the -Path key. The model is to be added to the Catalog referenced by the $metaStore and overwrites the existent version (if any) due to the -Force key.

    Despite the -Force key you can get the following error:

    Error: Cannot merge contents of LobSystem (External System) with Name 'Products'
    as it appears to be different from a preexisting LobSystem in the current load context.
    


    What you need to do in this case is remove the previous version of the model.


    References:

    http://dotnetfollower.com/wordpress/tag/business-data-connectivity/

    http://sharepoint.stackexchange.com/questions/109706/removing-a-bdc-catalog-using-powershell

    http://tmarchisio.wordpress.com/tag/business-data-catalog/

    Posted in PowerShell, Scripts, Sharepoint | Etiquetado: , , , | Leave a Comment »

    Modify Local Security Policy

    Posted by kiquenet en 29 abril 2014

    In Windows Server 2012.

    In Administrative Tools folder, double click the Local Security Policy icon, expand Account Policies and click Password Policy.

    In the right pane double click Password must meet complexity requirements and set it to Disabled. Click OK to save your policy change.

    Optionally you can also choose to never let passwords expire. To do this open the Maximum password age policy and set set the value to 0. Click OK to apply the change.

    Note: Just make sure to update the GP. go to Command Prompt and type GPUPDATE /FORCE

    Programmatically:

    http://stackoverflow.com/questions/23260656/modify-local-security-policy-using-powershell

    secedit /export /cfg c:\secpol.cfg
    (gc C:\secpol.cfg).replace("PasswordComplexity = 1", "PasswordComplexity = 0") | Out-File C:\secpol.cfg
    secedit /configure /db c:\windows\security\local.sdb /cfg c:\secpol.cfg /areas SECURITYPOLICY
    rm -force c:\secpol.cfg -confirm:$false


    http://vlasenko.org/2011/04/27/removing-password-complexity-requirements-from-windows-server-2008-core/
    secedit /export /cfg c:\new.cfg
    ${c:new.cfg}=${c:new.cfg} | % {$_.Replace('PasswordComplexity=1', 'PasswordComplexity=0')}
    secedit /configure /db $env:windir\security\new.sdb /cfg c:\new.cfg /areas SECURITYPOLICY
    del c:\new.cfg
     
    Referencias:
    http://www.win2012workstation.com/password-restrictions/
    http://windowsitpro.com/blog/securing-services-powershell
    http://sqldbamusings.blogspot.com.es/2012/03/powershell-adding-accounts-to-local.html
    http://code.msdn.microsoft.com/windowsdesktop/Add-log-on-as-a-service-a64dd63c
     

    Posted in PowerShell, Scripts, Trucos, Windows | Etiquetado: , | Leave a Comment »

    Convert DOC (Office 97-2003) to DOCX (Office 2010) programatically or Bulk

    Posted by kiquenet en 20 febrero 2014

    Option 1) OMPM – Using  Office File Converter (OFC) and Version Extraction Tool

    Download Office Migration Planning Manager (OMPM) to the machine that hosts the files.

    Office Migration Planning Manager (OMPM): Office Compatibility
    The Office Migration Planning Manager (OMPM) 2010 is a group of tools designed to help administrators during the planning and testing phases of a Microsoft Office 2010 or Office 365 deployment.
    http://www.microsoft.com/en-us/download/details.aspx?id=11454

    The installer extracts all tools and resource files by default to C:\OMPM

    If your machine does not have Office 2003 installed, which by now it probably does not, then you will also need to install the Office compatibility pack to convert the files.

    Módulo de compatibilidad de Microsoft Office para formatos de archivos de Word, Excel y PowerPoint
    http://support.microsoft.com/kb/923505


    Once that is installed, open the C:\OMPM\Tools folder that contains tools the Office File Converter (OFC) and Version Extraction Tool (VET)

    Using OMPM tools
    Use the Office File Converter (OFC) to do bulk conversions of specific Office files that are in version 97 through 2003 file formats to the Office 2010 file formats.
    http://technet.microsoft.com/en-us/library/cc179179(v=office.14).aspx

    Steps:

    Edit OFC.INI file.

    For the OFC.exe to work there must be a [FoldersToConvert] entry in the OFC.INI file.

    Fldr=C:\OMPM\TOOLS\EXAMPLE FOLDER\ (in this case I put all the files I wanted to convert into the “EXAMPLE FOLDER”)

    You can put multiple folders under this folder,


    OFC.exe will follow this path a convert all DOC and XLS files into DOCX and XLSX files.

    It will leave the old DOC file in the folder and create the new DOCX file right alongside.

    The date created field will NOT change for the new DOCX or XLSX files


    All the new files will be created in the appropriate folder, even if there are multiple layers of folders under the original location

    In this case, the SourcePathTemplate and the DestinationPathTemplate listings in the OFC.INI file are just:

    SourcePathTemplate=*\*\*\*\

    DestinationPathTemplate=*1\*2\*3\*4\


    Option 2) PowerShell

    Requires Office 2010 installed

    The main:

    $CompatMode = [Enum]::Parse([Microsoft.Office.Interop.Word.WdCompatibilityMode], "wdWord2010")
    $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat],"wdFormatDocumentDefault");

    Clear-Host
    $word = new-object -comobject word.application
    $word.Visible = $False

    Get-ChildItem -path $folderpath -recurse -include $fileType | % {

        SaveDocumentWithCompatibilityMode $word $_
        ConvertDocument $word $_
    }

    #Clean up
    $word.quit()
    $rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
    $word = $null
    [gc]::collect()
    [gc]::WaitForPendingFinalizers()

    Save Document with Compatibility

    Function SaveDocumentWithCompatibilityMode($word, $document)
    {
        $opendoc = $word.documents.open($document.FullName)
        $savename = ($document.Fullname).substring(0,($document.FullName).lastindexOf("."))
       
        $savename = $savename + "_test1.docx"
       
        $opendoc.saveas([ref]"$savename", [ref]$saveFormat);
        $opendoc.close();
        
        $converteddoc = Get-ChildItem -Path $savename
        $opendoc = $word.documents.open($converteddoc.FullName)
        $opendoc.SetCompatibilityMode($CompatMode);
        $opendoc.save()
        $opendoc.close()
    }


    I prefer Convert Document

    Function ConvertDocument($word, $document)
    {
        $opendoc = $word.documents.open($document.FullName)
        #$opendoc = $word.documents.open($document.FullName,$false,$true);
       
        $savename = ($document.fullname).substring(0,($document.FullName).lastindexOf("."))
        $savename = $savename + "_test2.docx"
        
        $opendoc.Convert()
        $opendoc.saveas([ref]"$savename", [ref]$saveFormat);
        $opendoc.close();
       
       
        # Stop Winword Process
        #$opendoc.quit()
        #$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($opendoc)
           
    }

    Referencias:
    http://mongell.blogspot.com.es/2013/11/using-ompm-or-how-to-bulk-convert-pre.html

    http://mykbit.blogspot.com.es/2011/10/bulk-convert-office-972003-files-to.html

    http://blogs.msdn.com/b/ericwhite/archive/2008/09/19/bulk-convert-doc-to-docx.aspx

    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/04718fc5-ab83-45d9-a413-e3cd9d18926f/how-to-convert-doc-files-to-docx-in-a-sharepoint-library-programmatically?forum=sharepointdevelopmentprevious

    http://row5is.wordpress.com/2012/07/06/convert-doc-to-docx-using-powershell/

    http://stackoverflow.com/questions/16534292/basic-powershell-batch-convert-word-docx-to-pdf

    http://blogs.technet.com/b/heyscriptingguy/archive/2010/06/22/hey-scripting-guy-how-can-i-use-windows-powershell-2-0-to-convert-doc-files-to-docx-files.aspx

    Posted in Herramientas, Productividad, Scripts | Etiquetado: , , , , | Leave a Comment »