Solved

load a file $INAME.ps1 containing variables

Posted on 2014-09-09
2
149 Views
Last Modified: 2014-09-25
Hello,

I try to load a file $INAME.ps1 containing variables. This file is in $CONF_INST_CONFIG directory which is detected automatically with the following code but it doesn't work:

The error returned is :
 The corresponding line is 'foreach ( $item in $INSTVAR) { . $item.FullName }'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'The expression after '.' in a pipeline element produced an invalid object. It must result in a command name, script block or CommandInfo object.  '.  Process Exit Code -1.  The step failed.


$Global:CONF_INST_TREE_ROOT=""
$Global:CONF_INST_CONFIG="$CONF_INST_TREE_ROOT\config"
$Global:CONF_INST_SCRIPT_LOG="$CONF_INST_TREE_ROOT\log"
$SERVERNAME = (Get-WmiObject Win32_Computersystem).Name
if ($I -contains "\") {
$INAME=$I.split("\")[1]
}
else {
$INAME="MSSQLSERVER"
}
$WORKDIR=(get-location).providerpath 
$GLOBAL=(Split-Path -parent $WORKDIR)
$GLOBALVAR = gci -Recurse ($GLOBAL) -in global.ps1 
$REGPATH=((REG QUERY "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" /v $INAME /reg:64 | Select-String -pattern $INAME) -split "\s+")[-1] 
$FULLPATH = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$REGPATH"
$SQLROOT = (Get-ItemProperty "$fullpath\Setup").SQLDataRoot
$CONF_INST_TREE_ROOT = ($SQLROOT.Split("\") | Select -First 3) -join ("\")

  if ($CONF_INST_TREE_ROOT -match $INAME) {
	$CONF_INST_CONFIG=$CONF_INST_TREE_ROOT + "\" + "config"
  } 
  else {
  Push-Location $CONF_INST_TREE_ROOT 
  $IROOT=Get-ChildItem $CONF_INST_TREE_ROOT  | ? { $_.PSIsContainer } | Where-Object { $_.name -match "$INAME"}
  $CONF_INST_CONFIG = $CONF_INST_TREE_ROOT + "\" + $IROOT + "\" + "config" 
  
  }

foreach ( $item in $GLOBALVAR) { . $item.FullName }
$INSTVAR = gci -Recurse ($CONF_INST_CONFIG) -in $INAME.ps1 

Open in new window


How can I resolve it?

Thanks
0
Comment
Question by:bibi92
2 Comments
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40314154
I don't think you need the '.' before $item.FullName .
Should be:
foreach ( $item in $INSTVAR) { $item.FullName }

Open in new window

0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 40314206
If you're trying to load the content of the file into the current session dot-sourcing is the right thing to do (or at least a right thing to do).

Perhaps echo out (Write-Host) the value you're trying to dot-source? Or use Test-Path? I suspect you're trying to feed a null value in; Looping on $null is something of a feature of PowerShell. You can get the same error if you run:
. $null

Open in new window

Chris
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
2016 SQL Licensing 7 40
Need help how to find where my error is in UFD 6 26
convert null in sql server 12 31
Sql Server group by 10 26
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A brief introduction to what I consider to be the best editor for PowerShell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question