简体   繁体   中英

Powershell script Issue string

The $Vendor is not recognised when pass it as paramater for creating folder in the console.It gives error String input not in correct format.If I pass any string manually it is recognizing and creating folder with that name.I tried parsing $Vendor to string.Could you help me?

    #function to fetch folder information
    function APP-Parameters($appfolder)
    {
    Write-Output $appfolder

    $data = $appfolder.split("-")
    $APPLICATIONNAME = $data[2]+$data[3]+$data[4]+$data[5]+$data[6]+$data[0]
    $DEPLOYMENTTYPENAME = $data[5]+$data[6]+$data[0]
    $obj= new-object psobject
    $obj | Add-Member Noteproperty APPID $data[0]
    $obj | Add-Member Noteproperty APPNAME $data[2]
    $obj | Add-Member Noteproperty VERSION $data[3]
    $obj | Add-Member Noteproperty LANGUAGE $data[4]
    $obj | Add-Member Noteproperty VARIANT $data[5]
    $obj | Add-Member Noteproperty RELEASEVERSION $data[6]
    #$obj | Add-Member Noteproperty APPLICATIONNAME $APPLICATIONNAME
    #$obj | Add-Member Noteproperty DEPLOYMENTTYPENAME $DEPLOYMENTTYPENAME



    switch ($data[1])
    {
    "MS" 
    {
    $obj | Add-Member Noteproperty VENDOR "Microsoft"
    break
    }
    default {
    $obj | Add-Member Noteproperty VENDOR $data[1]
    }

    }

    Return $obj


    }
    #Function for Creating the Folder
    Function New-CMFolder
    {
        [CmdLetBinding()]
        Param(
        [Parameter(Mandatory=$True,HelpMessage="Please Enter Site Server Site code")]
                  $SiteCode,
        [Parameter(Mandatory=$True,HelpMessage="Please Enter Site Server Name")]
                  $SiteServer,
        [Parameter(Mandatory=$True,HelpMessage="Please Enter Folder Name")]
                  $Name,
        [Parameter(Mandatory=$True,HelpMessage="Please Enter Folder Object Type")]
                  $ObjectType,
        [Parameter(Mandatory=$True,HelpMessage="Please Enter parent folder ID")]
                  $ParentContainerNodeId                                          
             )

        $Arguments = @{Name = $Name; ObjectType = "$ObjectType"; ParentContainerNodeId = "$ParentContainerNodeId"}
        Try{
            Set-WmiInstance -Namespace "root\sms\Site_$SiteCode" -Class "SMS_ObjectContainerNode" -Arguments $Arguments `
            -ComputerName $SiteServer -ErrorAction STOP
        }
        Catch{
            $_.Exception.Message
            "THE VENDOR FOLDER PRESENT" + $Name | out-file  C:\Windows\Logs\test.txt -append
        }          
    }
    #Function for Fetching the node
    function Get-ParentNode($ConsoleFolder)
    {
    $Confolder=$ConsoleFolder.split("\")
    $ParentContainerNodeId="0"
    $len=$Confolder.length

    For ($i=0; $i -lt $len; $i++)
    {
    New-CMFolder -SiteCode "PR1" -SiteServer $SiteServer -Name $Confolder[$i] -ObjectType "6000" -ParentContainerNodeId $ParentContainerNodeId
    $Parent_Node=Get-WMIObject -ComputerName $SiteServer -Namespace "root\sms\site_$siteCode" -Query "Select * From SMS_ObjectContainerNode" | Where { ($_.Name -like $Confolder[$i]) -and ($_.ObjectType -like '6000') }
    $ParentContainerNodeId = $Parent_Node.ContainerNodeID
    }
    $len=$len-1
    $Parent_Node=Get-WMIObject -ComputerName $SiteServer -Namespace "root\sms\site_$siteCode" -Query "Select * From SMS_ObjectContainerNode" | Where { ($_.Name -like $Confolder[$len]) -and ($_.ObjectType -like '6000') }
    $ParentContainerNodeId1 = $Parent_Node.ContainerNodeID
    $obj= new-object psobject
    $obj | Add-Member Noteproperty ParentContainerNodeId $ParentContainerNodeId1
    Return $obj
    }
    #Global Variable Declaration
    $SiteCode="PR1"
    $SiteServer="Babusccmlab1"
    $ObjectType="6000"
    $ConsoleFolder="Global Package\SRT-Packages"
    $ExportFilesLocation="\\babusccmlab1\c$\AZTESTPACKAGES\*.*"
    $ParentContainerNodeId="0"
    $Parent_Node=Get-ParentNode($ConsoleFolder)
    $Parent_Node=$Parent_Node.ParentContainerNodeID

    $ProductionServer="\\SMSSITESERVER\AZTESTPACKAGES\"
    Set-Location C:\
    $a= Get-Childitem  -path $ExportFilesLocation  -include *.zip 
    cd "Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin"
    $Setsite = $SiteCode + ":"
    CD $Setsite


    foreach($x in $a)  {
    If($x.name -match "^[0-9]{6}\-[a-z0-9-_+']*\-[a-z0-9-_+']*\-[a-z0-9.-_+']*\-[a-z0-9-_+']*\-[a-z0-9-_+']*\-[a-z0-9-_+']*\.ZIP")
    {
    $appfoldername = $x.name.TrimEnd(".zip")
    $APPID=APP-Parameters($appfoldername) | SELECT APPID | out-file  C:\Windows\Logs\test.txt -append
    $VENDOR=APP-Parameters($appfoldername)  
    [String]$VENDOR = $VENDOR.Vendor | out-file  C:\Windows\Logs\test.txt -append
    [String]$u = $Vendor  | out-file  C:\Windows\Logs\test.txt -append 
    $APPNAME=APP-Parameters($appfoldername) | SELECT APPNAME | out-file  C:\Windows\Logs\test.txt -append
    $LANGUAGE=APP-Parameters($appfoldername) | SELECT LANGUAGE | out-file  C:\Windows\Logs\test.txt -append
    $VARIANT=APP-Parameters($appfoldername) | SELECT VARIANT | out-file  C:\Windows\Logs\test.txt -append
    $SCCM_APPNAME=APP-Parameters($appfoldername) | SELECT APPLICATIONNAME | out-file  C:\Windows\Logs\test.txt -append
    #$SCCM_DEPLOYMENTTYPENAME=APP-Parameters($appfoldername) | SELECT DEPLOYMENTTYPENAME
    #$RELEASEVERSION=APP-Parameters($appfoldername) | SELECT RELEASEVERSION

     "--------" + $x.name + "--------" | out-file  C:\Windows\Logs\test.txt -append
     "" | out-file  C:\Windows\Logs\test.txt -append


    "THE FOLDER STRUCTURE  MATCH" | out-file  C:\Windows\Logs\test.txt -append
     "" | out-file  C:\Windows\Logs\test.txt -append 
     "" | out-file  C:\Windows\Logs\test.txt -append 
    #----------------------------------------------------SCCM2012 CONSOLE MANAGEMENT---------------------------------------------------------
    New-CMFolder -SiteCode $SiteCode -SiteServer $SiteServer -Name $Vendor -ObjectType $ObjectType -ParentContainerNodeId $Parent_Node


    } else
    {
     "--------" + $x.name + "--------" | out-file   C:\Windows\Logs\test.txt -append
     "" | out-file  C:\Windows\Logs\test.txt -append


    "THE FOLDER STRUCTURE DOES NOT MATCH" | out-file  C:\Windows\Logs\test.txt -append
     "" | out-file  C:\Windows\Logs\test.txt -append 
     "" | out-file  C:\Windows\Logs\test.txt -append
    }
    }

What line is it giving you the error on?

This line:

[String]$VENDOR = $VENDOR.Vendor | out-file  C:\Windows\Logs\test.txt -append

doesn't make any sense. That pipe operation isn't going to return anything, so $VENDOR is going to be null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM